module ObjectifyHash
Constants
- EXCEPTIONS
- NULLABLE_KEYS
- VERSION
Attributes
values_to_compare[RW]
Public Instance Methods
==(other)
click to toggle source
# File lib/objectifyhash.rb, line 100 def == other return false unless other.respond_to? :values_to_compare return false unless self.values_to_compare - other.values_to_compare == [] #should not use == cuz arrays can have different orders (self.values_to_compare - ignore_equal ).each do |value| return false unless self.method(value).() == other.method(value).() end return true end
Also aliased as: eql?
[](val)
click to toggle source
retro bunker compatibility
# File lib/objectifyhash.rb, line 116 def [] val $stderr.puts 'DEPRECATION WARNING #[] CALLED ON OBJECT' raise NameError unless self.respond_to?( val.to_sym ) self.method( val ).call end
convert_and_define(hash)
click to toggle source
# File lib/objectifyhash.rb, line 23 def convert_and_define hash @original_hash = hash @values_to_compare||=[] @original_hash.each do |key, value| key = 'klass' if key == 'class' key = key.to_sym case when key == :id set :guid, value when key == :method set :verb, value when EXCEPTIONS.key?( key ) set key, EXCEPTIONS[ key ].call( value ) when value.is_a?(Hash) klass = get_new_class key # puts "Going to instanciate class based on #{key}" object = klass.new( value ) # puts 'success' set key, object when value.is_a?(Array) set key, objectify_array(value, key) else set key, value end end NULLABLE_KEYS.each do |key| unless self.respond_to?( key ) set key, nil end end end
empty?()
click to toggle source
# File lib/objectifyhash.rb, line 139 def empty? self.values_to_compare.empty? end
get_new_class(name)
click to toggle source
# File lib/objectifyhash.rb, line 73 def get_new_class name name = "#{name.capitalize}_".to_sym if Object.constants.include? name.capitalize.to_sym name = name.to_s.sub(/^_/, '').to_sym self.class.const_defined?(name.capitalize) ? self.class.const_get(name.capitalize) : self.class.const_set(name.capitalize, ObjectifyHash::GenericObject) end
ignore_equal(key = nil)
click to toggle source
# File lib/objectifyhash.rb, line 94 def ignore_equal key = nil @ignore_equal||=[] @ignore_equal.push key if key @ignore_equal end
method_missing(m, *args, &block)
click to toggle source
Calls superclass method
# File lib/objectifyhash.rb, line 110 def method_missing m, *args, &block super m, *args, &block if args.length > 0 or block_given? nil end
objectify_array(array,name)
click to toggle source
# File lib/objectifyhash.rb, line 58 def objectify_array array,name array.map do |elem| if EXCEPTIONS.key? name EXCEPTIONS[ name ].call( elem ) else if elem.is_a? Hash klass = get_new_class name.to_s.sub(/s$/, '').to_sym klass.new elem else elem end end end end
set(key, value)
click to toggle source
# File lib/objectifyhash.rb, line 82 def set key, value method_name = Object.instance_methods.include?( key ) ? :"_#{key}_" : key.to_sym @values_to_compare.push method_name self.__send__(:define_singleton_method, method_name, Proc.new do value; end ) end
to_h()
click to toggle source
# File lib/objectifyhash.rb, line 122 def to_h h = {} values_to_compare.each do |m| if self.method( m ).().nil? and NULLABLE_KEYS.include?( m ) next end if self.method( m ).().respond_to? :values_to_compare h[ m.to_sym ] = self.method( m ).().to_h elsif self.method( m ).().is_a? Array h[ m.to_sym ] = un_objectify_array( self.method( m ).() ) else h[ m.to_sym ] = self.method( m ).() end end return h end
values_at(*args)
click to toggle source
# File lib/objectifyhash.rb, line 88 def values_at *args args.map do |key| self.respond_to?( key ) ? self.method( key ).call : nil end end
Private Instance Methods
un_objectify_array(array)
click to toggle source
# File lib/objectifyhash.rb, line 144 def un_objectify_array array array.map do |v| if v.is_a? Array un_objectify_array v elsif v.respond_to? :values_to_compare v.to_h else v end end end