lib/pp-adaptive/support/json_model.rb in pp-adaptive-0.0.2 vs lib/pp-adaptive/support/json_model.rb in pp-adaptive-0.0.3

- old
+ new

@@ -72,15 +72,15 @@ end private def attribute_key(key) - Hash[attributes.map { |attr| [attr.options.fetch(:param, attr.name).to_s, attr.name.to_sym] }][key] || key.to_sym + Hash[attribute_set.map { |attr| [attr.options.fetch(:param, attr.name).to_s, attr.name.to_sym] }][key] || key.to_sym end def attribute_value(key, value) - return unless attribute = attributes[attribute_key(key)] + return unless attribute = attribute_set[attribute_key(key)] case attribute when Node attribute.type.to_attributes(value) when NodeList @@ -96,12 +96,13 @@ # Recursively read all attributes in a ruby Hash, mapping attribute names to JSON parameters, according to the :param option # # @return [Hash] # the JSON representation in ruby Hash form def to_hash - attributes.inject({}) { |hash, (key, value)| value.nil? ? hash : hash.merge(json_key(key) => json_value(value)) } \ - .reject { |key, value| value.kind_of?(Enumerable) && value.none? } + Hash[attribute_set.map{ |a| [a.name, self[a.name]] }]. + inject({}) { |hash, (key, value)| value.nil? ? hash : hash.merge(json_key(key) => json_value(value)) }. + reject { |key, value| value.kind_of?(Enumerable) && value.none? } end # Convert this JsonModel into a JSON string for transport to the PayPal API # # @return [String] @@ -111,11 +112,11 @@ end private def json_key(key) - if self.class.attributes[key] - self.class.attributes[key].options.fetch(:param, key).to_s + if self.class.attribute_set[key] + self.class.attribute_set[key].options.fetch(:param, key).to_s else key.to_s end end