module Nucleon::Mixin::Macro::ObjectInterface

Public Instance Methods

object_collection(_type, _method_options = {}) click to toggle source
    # File lib/core/mixin/macro/object_interface.rb
 16 def object_collection(_type, _method_options = {})
 17   _method_config = Config.ensure(_method_options)
 18 
 19   _plural      = _method_config.init(:plural, "#{_type}s").get(:plural)
 20 
 21   unless _ensure_proc = _method_config.get(:ensure_proc, false)
 22     _ensure_proc = Proc.new {|name, options = {}| options }
 23   end
 24   _delete_proc = _method_config.get(:delete_proc)
 25   _search_proc = _method_config.get(:search_proc)
 26 
 27   @@object_types[_type] = _method_config
 28 
 29   logger.debug("Creating new object collection #{_type} with: #{_method_config.inspect}")
 30 
 31   #---------------------------------------------------------------------------
 32 
 33   object_utilities
 34 
 35   #---------------------------------------------------------------------------
 36 
 37   logger.debug("Defining object interface method: #{_type}_config")
 38 
 39   define_method "#{_type}_config" do |name = nil|
 40     Config.new((name ? get([ _plural, name ], {}) : get(_plural, {})), {}, true, false)
 41   end
 42 
 43   #---
 44 
 45   logger.debug("Defining object interface method: #{_type}_setting")
 46 
 47   define_method "#{_type}_setting" do |name, property, default = nil, format = false|
 48     get([ _plural, name, property ], default, format)
 49   end
 50 
 51   #---
 52 
 53   logger.debug("Defining object interface method: #{_plural}")
 54 
 55   define_method "#{_plural}" do |reset = false|
 56     send("init_#{_plural}") if reset || _get(_plural, {}).empty?
 57     _get(_plural, {})
 58   end
 59 
 60   #---
 61 
 62   logger.debug("Defining object interface method: init_#{_plural}")
 63 
 64   define_method "init_#{_plural}" do
 65     data = hash(_search_proc.call) if _search_proc
 66     data = get_hash(_plural) unless data
 67 
 68     logger.debug("Initializing object data: #{data.inspect}")
 69 
 70     symbol_map(data).each do |name, options|
 71       if name != :settings
 72         options[:object_container] = myself
 73 
 74         logger.debug("Initializing object: #{name}")
 75 
 76         obj = _ensure_proc.call(name, options)
 77         _set([ _plural, name ], obj)
 78       end
 79     end
 80   end
 81 
 82   #---
 83 
 84   logger.debug("Defining object interface method: set_#{_plural}")
 85 
 86   define_method "set_#{_plural}" do |data = {}|
 87     data = Config.ensure(data).export
 88 
 89     send("clear_#{_plural}")
 90     set(_plural, data)
 91 
 92     logger.debug("Setting #{_plural}")
 93 
 94     data.each do |name, options|
 95       options[:object_container] = myself
 96 
 97       logger.debug("Setting #{_type} #{name}: #{options.inspect}")
 98 
 99       obj = _ensure_proc.call(name, options)
100       _set([ _plural, name ], obj)
101     end
102   end
103 
104   #---
105 
106   logger.debug("Defining object interface method: #{_type}")
107 
108   define_method "#{_type}" do |name, reset = false|
109     if reset || _get([ _plural, name ], nil).nil?
110       options = get([ _plural, name ], nil)
111 
112       unless options.nil?
113         options[:object_container] = myself
114 
115         logger.debug("Initializing object: #{name}")
116 
117         obj = _ensure_proc.call(name, options)
118         _set([ _plural, name ], obj)
119       end
120     end
121     _get([ _plural, name ])
122   end
123 
124   #---
125 
126   logger.debug("Defining object interface method: set_#{_type}")
127 
128   define_method "set_#{_type}" do |name, options = {}|
129     options = Config.ensure(options).export
130 
131     set([ _plural, name ], options)
132 
133     options[:object_container] = myself
134 
135     logger.debug("Setting #{_type} #{_name}: #{options.inspect}")
136 
137     obj = _ensure_proc.call(name, options)
138     _set([ _plural, name ], obj)
139   end
140 
141   #---
142 
143   logger.debug("Defining object interface method: set_#{_type}_setting")
144 
145   define_method "set_#{_type}_setting" do |name, property, value = nil|
146     logger.debug("Setting #{name} property #{property} to #{value.inspect}")
147     set([ _plural, name, property ], value)
148   end
149 
150   #---
151 
152   logger.debug("Defining object interface method: delete_#{_type}")
153 
154   define_method "delete_#{_type}" do |name|
155     obj = send(_type, name)
156 
157     logger.debug("Deleting #{_type} #{name}")
158 
159     delete([ _plural, name ])
160     _delete([ _plural, name ])
161 
162     _delete_proc.call(obj) if _delete_proc && ! obj.nil?
163   end
164 
165   #---
166 
167   logger.debug("Defining object interface method: delete_#{_type}_setting")
168 
169   define_method "delete_#{_type}_setting" do |name, property|
170     logger.debug("Deleting #{name} property: #{property}")
171 
172     delete([ _plural, name, property ])
173   end
174 
175   #---
176 
177   logger.debug("Defining object interface method: clear_#{_plural}")
178 
179   define_method "clear_#{_plural}" do
180     get(_plural).keys.each do |name|
181       logger.debug("Clearing #{_type} #{name}")
182 
183       send("delete_#{_type}", name)
184     end
185   end
186 
187   #---------------------------------------------------------------------------
188 
189   logger.debug("Defining object interface method: search_#{_type}")
190 
191   define_method "search_#{_type}" do |name, keys, default = '', format = false, extra_groups = []|
192     obj_config = send("#{_type}_config", name)
193     search_object(obj_config, keys, default, format, extra_groups)
194   end
195 end
object_utilities() click to toggle source
    # File lib/core/mixin/macro/object_interface.rb
200 def object_utilities
201 
202   unless respond_to? :each_object_type
203     logger.debug("Defining object utility method: each_object_type")
204 
205     define_method :each_object_type do |object_types = nil, filter_proc = nil, &code|
206       object_types = @@object_types.keys unless object_types
207       object_types = [ object_types ] unless object_types.is_a?(Array)
208 
209       object_types.each do |type|
210         logger.debug("Processing object type: #{type}")
211 
212         unless filter_proc && ! filter_proc.call(type, @@object_types[type])
213           plural = @@object_types[type][:plural]
214 
215           logger.debug("Passing: #{@@object_types[type].inspect}")
216           code.call(type, plural, @@object_types[type])
217         end
218       end
219     end
220   end
221 
222   #---
223 
224   unless respond_to? :each_object
225     logger.debug("Defining object utility method: each_object")
226 
227     define_method :each_object do |object_types = nil, &code|
228       each_object_type(object_types) do |type, plural, options|
229         logger.debug("Processing object type #{type}/#{plural} with: #{options.inspect}")
230 
231         send(plural).each do |name, obj|
232           logger.debug("Processing object: #{name}")
233           code.call(type, name, obj)
234         end
235       end
236     end
237   end
238 
239   #---
240 
241   unless respond_to? :init_objects
242     logger.debug("Defining object utility method: init_objects")
243 
244     define_method :init_objects do |object_types = nil, filter_proc = nil|
245       logger.debug("Initializing object collection")
246 
247       each_object_type(object_types, filter_proc) do |type, plural, options|
248         send("init_#{plural}")
249       end
250     end
251   end
252 
253   #---
254 
255   unless respond_to? :clear_objects
256     logger.debug("Defining object utility method: clear_objects")
257 
258     define_method :clear_objects do |object_types = nil, filter_proc = nil|
259       logger.debug("Clearing object collection")
260 
261       each_object_type(object_types, filter_proc) do |type, plural, options|
262         send("clear_#{plural}")
263       end
264     end
265   end
266 
267   #---------------------------------------------------------------------------
268 
269   unless respond_to? :search_object
270     logger.debug("Defining object utility method: search_object")
271 
272     define_method :search_object do |obj_config, keys, default = nil, format = false, extra_groups = []|
273       obj_config = Marshal.load(Marshal.dump(obj_config))
274 
275       logger.debug("Searching object properties: #{obj_config.inspect}")
276 
277       # TODO: Figure out a way to effectively cache this search operation
278       #------------------------------------------------------------------
279 
280       add_settings = lambda do |final_options, groups|
281         if groups
282           local_options = {}
283           array(groups).each do |group_name|
284             if group_options = Marshal.load(Marshal.dump(settings(group_name.to_sym)))
285               if group_options.has_key?(:settings)
286                 group_options = add_settings.call(group_options, group_options[:settings])
287               end
288               local_options = Util::Data.merge([ local_options, group_options ], true, false)
289             end
290           end
291           unless local_options.empty?
292             final_options = Util::Data.merge([ local_options, final_options ], true, false)
293           end
294         end
295         final_options
296       end
297 
298       #---
299 
300       settings = {}
301 
302       keys = [ keys ] unless keys.is_a?(Array)
303       temp = keys.dup
304 
305       logger.debug("Searching object keys: #{keys.inspect}")
306 
307       logger.debug("Searching specialized settings")
308       until temp.empty? do
309         settings = add_settings.call(settings, obj_config.get([ temp, :settings ]))
310         temp.pop
311       end
312 
313       logger.debug("Specialized settings found: #{settings.inspect}")
314       logger.debug("Searching general settings")
315 
316       settings = add_settings.call(settings, [ obj_config.get_array(:settings), extra_groups ].flatten)
317 
318       #------------------------------------------------------------------
319       # TODO: Cache the above!!!
320 
321       logger.debug("Final settings found: #{settings.inspect}")
322 
323       if settings.empty?
324         value = obj_config.get(keys)
325       else
326         final_config = Config.new(Util::Data.merge([
327           Util::Data.clean(settings),
328           Util::Data.clean(obj_config.export)
329         ], true, false))
330         value = final_config.get(keys)
331 
332         logger.debug("Final configuration: #{final_config.export.inspect}")
333       end
334 
335       value = default if Util::Data.undef?(value)
336 
337       logger.debug("Final value found (format: #{format.inspect}): #{value.inspect}")
338       filter(value, format)
339     end
340   end
341 
342   #---------------------------------------------------------------------------
343   # Configuration loading saving
344 
345   unless respond_to? :load
346     logger.debug("Defining object utility method: load")
347 
348     define_method :load do |options = {}|
349       logger.debug("Loading configuration if possible")
350       if config.respond_to?(:load)
351         clear_objects
352         config.load(options)
353       end
354     end
355   end
356 
357   #---
358 
359   unless respond_to? :save
360     logger.debug("Defining object utility method: save")
361 
362     define_method :save do |options = {}|
363       logger.debug("Saving configuration if possible")
364       config.save(options) if config.respond_to?(:save)
365     end
366   end
367 end