Module: JSONMapper::ClassMethods
- Defined in:
- lib/json_mapper.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #json_attribute(name, *args) ⇒ Object
- #json_attributes(name, *args) ⇒ Object
- #json_data ⇒ Object
- #json_data=(data) ⇒ Object
-
#parse(data, options = {}) ⇒ Object
def json_data @json_data || [] end.
- #parse_collection(data, options = {}) ⇒ Object
- #parse_json(json) ⇒ Object
- #parse_json_collection(json) ⇒ Object
Instance Method Details
#attributes ⇒ Object
55 56 57 |
# File 'lib/json_mapper.rb', line 55 def attributes @attributes[to_s] || [] end |
#json_attribute(name, *args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/json_mapper.rb', line 33 def json_attribute(name, *args) source_attributes, type, = extract_attribute_data(name, *args) attribute = Attribute.new(name, source_attributes, type, ) @attributes[to_s] ||= [] @attributes[to_s] << attribute attr_accessor attribute.method_name.to_sym end |
#json_attributes(name, *args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/json_mapper.rb', line 44 def json_attributes(name, *args) source_attributes, type, = extract_attribute_data(name, *args) attribute = AttributeList.new(name, source_attributes, type, ) @attributes[to_s] ||= [] @attributes[to_s] << attribute attr_accessor attribute.method_name.to_sym end |
#json_data ⇒ Object
29 30 31 |
# File 'lib/json_mapper.rb', line 29 def json_data @json_data[to_s] end |
#json_data=(data) ⇒ Object
25 26 27 |
# File 'lib/json_mapper.rb', line 25 def json_data=(data) @json_data[to_s] = data end |
#parse(data, options = {}) ⇒ Object
def json_data
@json_data[to_s] || []
end
63 64 65 66 67 68 69 |
# File 'lib/json_mapper.rb', line 63 def parse(data, = {}) return nil if data.nil? || data == "" json = get_json_structure(data, ) parse_json(json) end |
#parse_collection(data, options = {}) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/json_mapper.rb', line 71 def parse_collection(data, = {}) return [] if data.nil? || data == "" json = get_json_structure(data, ) parse_json_collection(json) end |
#parse_json(json) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/json_mapper.rb', line 79 def parse_json(json) # Set the JSON data for this instance self.json_data = json # @json_data[to_s] = json # Create a new instance of ourselves instance = new # Instantiate all AttributeList instances attributes.each do |attribute| if attribute.is_a?(AttributeList) instance.send("#{attribute.name}=", attribute.dup) end end # Traverse all defined attributes and assign data from the # JSON data structure attributes.each do |attribute| if is_mapped?(attribute, json) value = mapping_value(attribute, json) if attribute.is_a?(AttributeList) value = [ value ] unless value.is_a?(Array) value.each do |v| list_attribute = build_attribute(attribute.name, attribute.type, attribute.) list_attribute_value = list_attribute.typecast(v) # Some times typecasting a value for a list will produce another list, in the case of # for instance DelimitedString. If this is the case, we concat that array to the list. # Otherwise, we just append the value. if list_attribute_value.is_a?(Array) instance.send("#{attribute.name}").concat(list_attribute_value) else instance.send("#{attribute.name}") << list_attribute_value end end else instance.send("#{attribute.name}=".to_sym, attribute.typecast(value)) end end end instance end |
#parse_json_collection(json) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/json_mapper.rb', line 127 def parse_json_collection(json) collection = [] if json.is_a?(Array) json.each do |element| collection << parse_json(element) end end collection end |