class MightyStruct
Constants
- VERSION
Public Class Methods
new(object, options = {})
click to toggle source
# File lib/mighty_struct.rb, line 13 def initialize(object, options = {}) unless self.class.new?(object) raise ArgumentError.new("Cannot create a an instance of #{self.class} for the given object!") end @cache = options[:caching] == :enabled ? {} : nil if (@object = object).respond_to?(:keys) object.keys.each do |_key| unless respond_to?(_key) define_singleton_method(_key) do if @cache @cache[_key] ||= self.class.new?(value = @object[_key]) ? self.class.new(value) : value else self.class.new?(value = @object[_key]) ? self.class.new(value) : value end end end end end end
new?(object)
click to toggle source
# File lib/mighty_struct.rb, line 4 def self.new?(object) object.is_a?(Enumerable) end
to_object(object)
click to toggle source
in order not to pollute the instance's method namespace this is a class method
# File lib/mighty_struct.rb, line 9 def self.to_object(object) object.is_a?(self) ? object.instance_variable_get(:@object) : object end
Public Instance Methods
method_missing(method_name, *arguments, &block)
click to toggle source
last line of defense
Calls superclass method
# File lib/mighty_struct.rb, line 38 def method_missing(method_name, *arguments, &block) if @object.respond_to?(method_name) @cache.clear if @cache # clear the properties cache, because the called method may have side-effects result = @object.send(method_name, *arguments, &block) # ensure that results of called methods are mighty structs again self.class.new?(result) ? self.class.new(result) : result else super end end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/mighty_struct.rb, line 50 def respond_to_missing?(method_name, include_private = false) @object.respond_to?(method_name) || super end