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
- #parse(data, options = {}) ⇒ Object
- #parse_collection(data, options = {}) ⇒ Object
- #parse_json(json) ⇒ Object
- #parse_json_collection(json) ⇒ Object
Instance Method Details
#attributes ⇒ Object
38 39 40 |
# File 'lib/json_mapper.rb', line 38 def attributes @attributes[to_s] || [] end |
#json_attribute(name, *args) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/json_mapper.rb', line 16 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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/json_mapper.rb', line 27 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
42 43 44 |
# File 'lib/json_mapper.rb', line 42 def json_data @json_data[to_s] || [] end |
#parse(data, options = {}) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/json_mapper.rb', line 46 def parse(data, = {}) return nil if data.nil? || data == "" json = get_json_structure(data, ) parse_json(json) end |
#parse_collection(data, options = {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/json_mapper.rb', line 54 def parse_collection(data, = {}) return [] if data.nil? || data == "" json = get_json_structure(data, ) parse_json_collection(json) end |
#parse_json(json) ⇒ Object
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 103 104 105 106 107 |
# File 'lib/json_mapper.rb', line 62 def parse_json(json) # Set the JSON data for this instance @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
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/json_mapper.rb', line 109 def parse_json_collection(json) collection = [] if json.is_a?(Array) json.each do |element| collection << parse_json(element) end end collection end |