Module: ExtendObject
- Defined in:
- lib/buzztools/extend_object.rb
Overview
this pattern of declaring methods in a module and then including into a class means that the methods can be used directly from the module
eg. ExtendObject::attr_from_json(json,object)
or on the class it is included into
eg. model.attr_from_json(json)
Class Method Summary collapse
-
.attr_from_json(aJsonObject, aObject = nil) ⇒ Object
only give second argument when used like ExtendObject::attr_from_json.
- .g?(*args) ⇒ Boolean
Class Method Details
.attr_from_json(aJsonObject, aObject = nil) ⇒ Object
only give second argument when used like ExtendObject::attr_from_json
11 12 13 14 15 16 17 18 |
# File 'lib/buzztools/extend_object.rb', line 11 def attr_from_json(aJsonObject,aObject=nil) # only give second argument when used like ExtendObject::attr_from_json aObject ||= self rubyObj = JSON.parse!(aJsonObject) rubyObj.each do |k,v| aObject.send k.to_sym,v end aObject end |
.g?(*args) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/buzztools/extend_object.rb', line 20 def g?(*args) if args.length==1 path = args.first else path = args end if path.is_a?(String) segments = path.split('.').map!(&:to_sym) segment = segments.shift elsif path.is_a?(Symbol) segments = [] segment = path elsif path.is_a?(Array) segments = path segment = segments.shift segment = segment.to_sym if segment end return self unless segment.to_nil value = if self.respond_to?(segment) self.send(segment) elsif self.respond_to?(:[]) (self[segment] || self[segment.to_s]) rescue nil end if segments.empty? value else value.respond_to?(:g?) ? value.g?(segments) : nil end end |