Class: ClassKit::Helper
- Inherits:
-
Object
- Object
- ClassKit::Helper
- Defined in:
- lib/class_kit/helper.rb
Instance Method Summary collapse
-
#from_hash(hash:, klass:, use_alias: false) ⇒ Object
This method is called to convert a Hash into a ClassKit object.
-
#from_json(json:, klass:, use_alias: false) ⇒ Object
This method is called to convert JSON into a ClassKit object.
-
#initialize ⇒ Helper
constructor
A new instance of Helper.
- #is_class_kit?(klass) ⇒ Boolean
- #is_class_kit_custom_type?(klass) ⇒ Boolean
-
#to_hash(object, use_alias = false) ⇒ Object
This method is called to convert a ClassKit object into a Hash.
-
#to_json(object, use_alias = false) ⇒ Object
This method is called to convert a ClassKit object into JSON.
- #validate_class_kit(klass) ⇒ Object
Constructor Details
#initialize ⇒ Helper
Returns a new instance of Helper.
4 5 6 7 8 |
# File 'lib/class_kit/helper.rb', line 4 def initialize @hash_helper = HashKit::Helper.new @attribute_helper = ClassKit::AttributeHelper.new @value_helper = ClassKit::ValueHelper.new end |
Instance Method Details
#from_hash(hash:, klass:, use_alias: false) ⇒ Object
This method is called to convert a Hash into a ClassKit object.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/class_kit/helper.rb', line 61 def from_hash(hash:, klass:, use_alias: false) validate_class_kit(klass) return hash.map { |i| from_hash(hash: i, klass: klass, use_alias: use_alias) } if hash.is_a?(Array) @hash_helper.indifferent!(hash) entity = klass.new attributes = @attribute_helper.get_attributes(klass) attributes.each do |attribute| key = use_alias ? (attribute[:alias] || attribute[:name]) : attribute[:name] type = attribute[:type] # if the hash value is nil skip it next if hash[key].nil? value = if is_class_kit?(type) from_hash(hash: hash[key], klass: type, use_alias: use_alias) elsif is_class_kit_custom_type?(type) type.parse_from_hash(hash[key]) elsif type == Array hash[key].map do |array_element| if attribute[:collection_type].nil? array_element else if is_class_kit?(attribute[:collection_type]) from_hash(hash: array_element, klass: attribute[:collection_type], use_alias: use_alias) elsif is_class_kit_custom_type?(attribute[:collection_type]) attribute[:collection_type].parse_from_hash(array_element) else @value_helper.parse(type: attribute[:collection_type], value: array_element) end end end else hash[key] end entity.public_send(:"#{attribute[:name]}=", value) end entity end |
#from_json(json:, klass:, use_alias: false) ⇒ Object
This method is called to convert JSON into a ClassKit object.
111 112 113 114 |
# File 'lib/class_kit/helper.rb', line 111 def from_json(json:, klass:, use_alias: false) hash = JSON.load(json) from_hash(hash: hash, klass: klass, use_alias: use_alias) end |
#is_class_kit?(klass) ⇒ Boolean
10 11 12 |
# File 'lib/class_kit/helper.rb', line 10 def is_class_kit?(klass) klass.is_a?(ClassKit) end |
#is_class_kit_custom_type?(klass) ⇒ Boolean
14 15 16 |
# File 'lib/class_kit/helper.rb', line 14 def is_class_kit_custom_type?(klass) !klass.nil? && klass != :bool && klass.include?(ClassKit::CustomType) end |
#to_hash(object, use_alias = false) ⇒ Object
This method is called to convert a ClassKit object into a Hash.
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 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/class_kit/helper.rb', line 24 def to_hash(object, use_alias = false) return object.map { |i| to_hash(i, use_alias) } if object.is_a?(Array) validate_class_kit(object.class) hash = {} attributes = @attribute_helper.get_attributes(object.class) attributes.each do |attribute| key = use_alias ? (attribute[:alias] || attribute[:name]) : attribute[:name] type = attribute[:type] value = object.public_send(attribute[:name]) if value != nil hash[key] = if is_class_kit?(type) to_hash(value, use_alias) elsif is_class_kit_custom_type?(type) value.to_hash_value elsif type == Array value.map do |i| if is_class_kit?(i.class) to_hash(i, use_alias) elsif is_class_kit_custom_type?(i.class) i.to_hash_value else i end end else value end end end @hash_helper.indifferent!(hash) hash end |
#to_json(object, use_alias = false) ⇒ Object
This method is called to convert a ClassKit object into JSON.
105 106 107 108 |
# File 'lib/class_kit/helper.rb', line 105 def to_json(object, use_alias = false) hash = to_hash(object, use_alias) JSON.dump(hash) end |
#validate_class_kit(klass) ⇒ Object
18 19 20 21 |
# File 'lib/class_kit/helper.rb', line 18 def validate_class_kit(klass) is_class_kit?(klass) || raise(ClassKit::Exceptions::InvalidClassError, "Class: #{klass} does not implement ClassKit.") end |