class Hashematics::Type
A Type
defines an object.
Constants
- HASH_VALUE
- OPEN_STRUCT_VALUE
Attributes
name[R]
object_class[R]
properties[R]
Public Class Methods
new(name: '', properties: nil, object_class: nil)
click to toggle source
# File lib/hashematics/type.rb, line 24 def initialize(name: '', properties: nil, object_class: nil) @name = name @properties = make_properties(properties) @object_class = object_class || HASH_VALUE freeze end
null_type()
click to toggle source
# File lib/hashematics/type.rb, line 14 def null_type @null_type ||= new end
Public Instance Methods
convert(object, child_hash = {})
click to toggle source
# File lib/hashematics/type.rb, line 32 def convert(object, child_hash = {}) make_object(to_hash(object).merge(child_hash)) end
Private Instance Methods
default_properties(object)
click to toggle source
# File lib/hashematics/type.rb, line 44 def default_properties(object) if object.respond_to?(:keys) make_properties(object.keys) else [] end end
make_object(hash)
click to toggle source
# File lib/hashematics/type.rb, line 52 def make_object(hash) if object_class.to_s == HASH_VALUE hash elsif object_class.to_s == OPEN_STRUCT_VALUE OpenStruct.new(hash) elsif object_class.is_a?(Proc) object_class.call(hash) else object_class.new(hash) end end
make_properties(val)
click to toggle source
# File lib/hashematics/type.rb, line 64 def make_properties(val) if val.is_a?(Array) || val.is_a?(String) || val.is_a?(Symbol) Array(val).map { |v| [v, v] }.to_h else val end end
to_hash(object)
click to toggle source
# File lib/hashematics/type.rb, line 38 def to_hash(object) (properties || default_properties(object)).map do |property, key| [property, ObjectInterface.get(object, key)] end.to_h end