Advertisement
TetraSource

Moon - Example

Feb 27th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local moon = require("moon")
  2.  
  3. local super = moon.super
  4. local Property = moon.Property
  5. local classmethod = moon.classmethod
  6.  
  7. local function class(...)
  8.     local c = moon.class(...)
  9.     _ENV[c.__name] = c
  10.     _ENV.C = c
  11. end
  12.  
  13. class("Food") do
  14.     local C = C -- localize class for super
  15.  
  16.     function C:__init()
  17.         self:make()
  18.     end
  19.  
  20.     -- empty since we need a more specific type of food
  21.     function C:make()
  22.     end
  23.  
  24.     C.taste = Property(
  25.         function(self)
  26.             return 0
  27.         end
  28.     )
  29. end
  30.  
  31. class("Noodles", Food) do
  32.     local C = C
  33.  
  34.     function C:__init(cookTime)
  35.         checkArg(1, cookTime, "number")
  36.         self._cookTime = cookTime
  37.         super(C, self).__init(self)
  38.     end
  39.  
  40.     function C:make()
  41.         super(C, self).make(self)
  42.         print(string.format("cook noodles for %d minutes", self._cookTime))
  43.     end
  44.  
  45.     C.taste = Property(
  46.         function(self)
  47.             return super(C, self).taste + 10 - math.abs(10 - self._cookTime)
  48.         end
  49.     )
  50. end
  51.  
  52. class("Sauce", Food) do
  53.     local C = C
  54.  
  55.     C.sauceTypes = {
  56.         -- the position determines the taste
  57.         "tomato",
  58.         "cheese",
  59.         "meat",
  60.     }
  61.  
  62.     function C:__init(sauceType, ...)
  63.         if self:getSauceTaste(sauceType) == 0 then
  64.             error("Invalid sauce type", 2)
  65.         end
  66.         self._sauceType = sauceType
  67.         super(C, self).__init(self, ...)
  68.     end
  69.  
  70.     function C:make()
  71.         super(C, self).make(self)
  72.         print(string.format("cook %s sauce ", self._sauceType))
  73.     end
  74.  
  75.     C.taste = Property(
  76.         function(self)
  77.             return super(C, self).taste + self:getSauceTaste(self._sauceType)
  78.         end
  79.     )
  80.  
  81.     -- class method
  82.     C.getSauceTaste = classmethod(
  83.     function(cls, sauceType)
  84.         -- this is inefficient but for this example okay
  85.         for i, ty in ipairs(cls.sauceTypes) do
  86.             if ty == sauceType then
  87.                 return i * 10
  88.             end
  89.         end
  90.         return 0
  91.     end)
  92. end
  93.  
  94. class("Pasta", Sauce, Noodles) do
  95.     local C = C
  96.  
  97.     function C:make()
  98.         print("cook pasta:")
  99.         super(C, self).make(self)
  100.     end
  101. end
  102.  
  103. print(Pasta:getSauceTaste("cheese"))
  104. print()
  105.  
  106. local pasta
  107. pasta = Noodles(15)
  108. print(pasta.taste)
  109. print()
  110.  
  111. pasta = Pasta("tomato", 5)
  112. print(pasta.taste)
  113. print()
  114.  
  115. pasta = Pasta("meat", 10)
  116. print(pasta.taste)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement