Module: JSONConvertible::ClassMethods
- Defined in:
- lib/json_convertible.rb
Overview
add these as class methods
Instance Method Summary collapse
-
#attribute_key_name_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to use a custom mapping of the attributes to JSON keys.
-
#attribute_name_to_json_proc_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to encode the result of the class attributes in a certain format into the JSON.
-
#attribute_to_type_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to provide the encoder information about which types are each attribute of the class.
- #from_json!(json_object) ⇒ Object
-
#json_to_attribute_name_proc_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to decode the JSON values back to the original types of the class attributes.
-
#map_enumerable_type(enumerable_property_name: nil, current_json_object: nil) ⇒ Any
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to provide a custom mapping for a enumerable property in the JSON.
Instance Method Details
#attribute_key_name_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to use a custom mapping of the attributes to JSON keys.
@example
def self.attribute_key_name_map
return { :@some_key => "some_key_in_json" }
end
116 117 118 |
# File 'lib/json_convertible.rb', line 116 def attribute_key_name_map return {} end |
#attribute_name_to_json_proc_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to encode the result of the class attributes in a certain format into the JSON.
@example
def self.attribute_name_to_json_proc_map
= proc { ||
.strftime('%Q')
}
return { :@timestamp => }
end
136 137 138 |
# File 'lib/json_convertible.rb', line 136 def attribute_name_to_json_proc_map return {} end |
#attribute_to_type_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to provide the encoder information about which types are each attribute of the class.
@example
def attribute_to_type_map
return { :@string_attribute => String, :@custom_class_attribute => CustomClass }
end
174 175 176 |
# File 'lib/json_convertible.rb', line 174 def attribute_to_type_map return {} end |
#from_json!(json_object) ⇒ Object
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/json_convertible.rb', line 76 def from_json!(json_object) instance, json_object = _initialize_using!(json_object) json_object.each do |var, val| # If we encounter with a value that is represented by an array, iterate over it. if val.kind_of?(Array) # For each of the objects in the array, we call the protected method to build the object array. array_property = [] array_name = nil val.each do |array_val| array_name, this_instance = _from_json!(var, array_val, is_iterable: true) array_property << this_instance end if array_name.nil? if attribute_key_name_map.key(var) array_name = attribute_key_name_map.key(var).to_sym else array_name = "@#{var}".to_sym end end instance.instance_variable_set(array_name, array_property) else var_name, var_value = _from_json!(var, val) instance.instance_variable_set(var_name, var_value) end end return instance end |
#json_to_attribute_name_proc_map ⇒ Hash
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to decode the JSON values back to the original types of the class attributes.
@example
def self.json_to_attribute_name_proc_map
= proc { |json|
Time.at(json.to_i)
}
return { :@timestamp => }
end
156 157 158 |
# File 'lib/json_convertible.rb', line 156 def json_to_attribute_name_proc_map return {} end |
#map_enumerable_type(enumerable_property_name: nil, current_json_object: nil) ⇒ Any
class method This method is intended to be overridden by any class that implements ‘JSONConvertible` and need to provide a custom mapping for a enumerable property in the JSON.
196 197 198 |
# File 'lib/json_convertible.rb', line 196 def map_enumerable_type(enumerable_property_name: nil, current_json_object: nil) return nil end |