svlad

Check-in [735070cae7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Changed Jo's cloning to using metamethods as described in PIL.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:735070cae7c511023d21312603e54f5cdb981a7c
User & Date: severak 2012-04-21 19:55:55
Context
2012-04-21
22:38
Added color object to jo.geometry check-in: 8db91da57b user: severak tags: trunk
19:55
Changed Jo's cloning to using metamethods as described in PIL. check-in: 735070cae7 user: severak tags: trunk
2012-04-16
20:15
Added first action. check-in: 25bf48a75d user: severak tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to jo.lua.

20
21
22
23
24
25
26

27

28
29
30
31
32
33
34
..
84
85
86
87
88
89
90
91



92
93
94
95
96
97
98
99
100
101





102
103
104
105
106
107
108
109
110
111
112
113
...
115
116
117
118
119
120
121


122
123

124
125
126
127
128
129





















130
131
132
--author: RCIX from stackoverflow
--based on code by Doug Currie
--http://stackoverflow.com/questions/1283388/lua-merge-tables
function tableMerge(t1, t2)
    for k,v in pairs(t2) do
        if type(v) == "table" then
                if type(t1[k] or false) == "table" then

                        tableMerge(t1[k] or {}, t2[k] or {})

                else
                        t1[k] = v
                end
        else
                t1[k] = v
        end
    end
................................................................................
      end
    })
]]
    }    
  },
--------------------------------------------------------------------------------  
  clone=function(self)
    local new=deepcopy(self)



    return new  
  end,
  
  rewriteSlots=function(self,add)
    return tableMerge(self,add)
  end 
}


helpful={





  help=function(self,theme)
    local chapters=self._H
    
    if not chapters then
       return false
    end
    
    if theme then
      if chapters[theme] then
        chapters=chapters[theme]
      else
         print("Undocumented feature or missing function. Object help shown instead.")
................................................................................
    end
    
    if chapters._Name~=nil then
      print(chapters._Name)
      print(string.rep("=",string.len(chapters._Name)))
    end
    


    for k,v in pairs(chapters) do
      if string.sub(k,1,1)=="_" and k~="_Name" then

        print("##"..string.sub(k,2))
        print(v)
        print("")
      end
    end
  end





















}

jo.object:rewriteSlots(jo.helpful)







>

>







 







|
>
>
>










>
>
>
>
>




|







 







>
>
|
<
>
|
|



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
...
125
126
127
128
129
130
131
132
133
134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
--author: RCIX from stackoverflow
--based on code by Doug Currie
--http://stackoverflow.com/questions/1283388/lua-merge-tables
function tableMerge(t1, t2)
    for k,v in pairs(t2) do
        if type(v) == "table" then
                if type(t1[k] or false) == "table" then
                        local mt=getmetatable(t1[k])
                        tableMerge(t1[k] or {}, t2[k] or {})
                        setmetatable(t1[k],mt)
                else
                        t1[k] = v
                end
        else
                t1[k] = v
        end
    end
................................................................................
      end
    })
]]
    }    
  },
--------------------------------------------------------------------------------  
  clone=function(self)
    local new={}
    setmetatable(new,{__index=self})
    new._H={}
    setmetatable(new._H,{__index=self._H})
    return new  
  end,
  
  rewriteSlots=function(self,add)
    return tableMerge(self,add)
  end 
}


helpful={
  _H={
    help={_basic="Print help from object internal documentation to console.",_usage="Call\n\n    object:help()\n\nor\n\n    object:help('methodName')"},
    getSlots={_basic="Print list of names of enabled slots to console."}
  },

  help=function(self,theme)
    local chapters=self._H
    
    if not chapters then
       print("No help aviable.")
    end
    
    if theme then
      if chapters[theme] then
        chapters=chapters[theme]
      else
         print("Undocumented feature or missing function. Object help shown instead.")
................................................................................
    end
    
    if chapters._Name~=nil then
      print(chapters._Name)
      print(string.rep("=",string.len(chapters._Name)))
    end
    
    local names={"_basic","_usage","_more","_seealso","_example","_notes","_version"}
    
    for k,v in pairs(names) do

      if chapters[v]~=nil then
        print("##"..string.sub(v,2))
        print(chapters[v])
        print("")
      end
    end
  end,
  
  getSlots=function(self,visited)
    if not visited then
      visited={}
    end
    
    for k,v in pairs(self) do
      if visited[k]==nil then
        print(" - "..k.." ("..type(v)..")")
        visited[k]=1
      end  
    end
    local mt=getmetatable(self)
    if type(mt)=="table" and type(mt.__index)=="table" then
      if type(mt.__index.getSlots)=="function" then
        mt.__index:getSlots(visited)
      end
    end
  end
  
  
}

jo.object:rewriteSlots(jo.helpful)

Changes to zoo.lua.

39
40
41
42
43
44
45
46
47
Jarku=crow:clone()
Jarku.name="Jarku"

Jarku:breath()
Jarku:beep()
print("Jarku is "..Jarku.color..".")

Jarku:rewriteSlots(jo.helpful):help()








|

39
40
41
42
43
44
45
46
47
Jarku=crow:clone()
Jarku.name="Jarku"

Jarku:breath()
Jarku:beep()
print("Jarku is "..Jarku.color..".")

--Jarku:rewriteSlots(jo.helpful):help()