Class: SavonHelper::MappingObject Abstract
- Extended by:
- DSL
- Defined in:
- lib/savon_helper/mapping_object.rb
Overview
MappingObject is an abstract class providing methods to automatically convert from and to savon data.
Direct Known Subclasses
Constant Summary collapse
- BLACK_LIST =
[:@interface]
- @@type_mappings =
Hash.new { |hash, key| hash[key] = Hash.new }
Instance Attribute Summary collapse
-
#interface ⇒ Object
readonly
Returns the value of attribute interface.
Attributes included from DSL
Mapping collapse
-
.all_type_mappings ⇒ Hash{Symbol => TypeMapping}
Return TypeMappings.
-
.defined_attributes ⇒ Array<Symbol>
Accessors defined by TypeMappings.
-
.from_savon(data, interface) ⇒ MappingObject
Return an initialized instance with the values from the (type-converted) hash.
-
.has_attribute_chain(field) ⇒ Boolean
Test if the given class understands the field definition.
-
.type_mappings ⇒ Hash{Symbol => TypeMapping}
Return TypeMappings specific to the class.
-
#to_json(*a) ⇒ Object
Convert the instance to a JSON representation.
-
#to_savon ⇒ Hash
Return the instance as a hash of simple (type-converted) values suitable for Savon.
Instance Method Summary collapse
-
#initialize(interface = nil) ⇒ MappingObject
constructor
A new instance of MappingObject.
- #to_s ⇒ Object
Methods included from DSL
array_boolean_accessor, array_datetime_accessor, array_double_accessor, array_enum_accessor, array_float__accessor, array_integer_accessor, array_ip_address_accessor, array_object_accessor, array_string_accessor, attr_boolean_accessor, attr_datetime_accessor, attr_double_accessor, attr_enum_accessor, attr_float_accessor, attr_integer_accessor, attr_ip_address_accessor, attr_object_accessor, attr_string_accessor, hint_object_accessor
Constructor Details
#initialize(interface = nil) ⇒ MappingObject
Returns a new instance of MappingObject.
14 15 16 |
# File 'lib/savon_helper/mapping_object.rb', line 14 def initialize(interface=nil) @interface = interface end |
Instance Attribute Details
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
8 9 10 |
# File 'lib/savon_helper/mapping_object.rb', line 8 def interface @interface end |
Class Method Details
.all_type_mappings ⇒ Hash{Symbol => TypeMapping}
Return TypeMappings.
64 65 66 |
# File 'lib/savon_helper/mapping_object.rb', line 64 def self.all_type_mappings self.superclass.all_type_mappings.merge(type_mappings()) end |
.defined_attributes ⇒ Array<Symbol>
Accessors defined by TypeMappings
97 98 99 |
# File 'lib/savon_helper/mapping_object.rb', line 97 def self.defined_attributes() self.type_mappings.keys end |
.from_savon(data, interface) ⇒ MappingObject
Return an initialized instance with the values from the (type-converted) hash.
36 37 38 39 40 41 42 |
# File 'lib/savon_helper/mapping_object.rb', line 36 def self.from_savon(data, interface) instance = self.new(interface) data.each do |key, value| instance.instance_variable_set("@#{key}", self.map_to_native(key, value, interface)) end instance end |
.has_attribute_chain(field) ⇒ Boolean
Test if the given class understands the field definition
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/savon_helper/mapping_object.rb', line 71 def self.has_attribute_chain(field) return true if self.method_defined?(field) current_class = self field.split('.').map(&:to_sym).all? do |each| # puts "Current Class: #{current_class.inspect}, Field: #{each}" if current_class.method_defined?(each) if current_class.respond_to?(:type_mappings) current_mapping = current_class.all_type_mappings[each] # puts "Mapping: #{current_mapping}" if !current_mapping.nil? current_class = current_mapping.object_klass else current_class = NilClass end else current_class = NilClass end true else false end end end |
.type_mappings ⇒ Hash{Symbol => TypeMapping}
Return TypeMappings specific to the class
58 59 60 |
# File 'lib/savon_helper/mapping_object.rb', line 58 def self.type_mappings @@type_mappings[self] end |
Instance Method Details
#to_json(*a) ⇒ Object
Convert the instance to a JSON representation
102 103 104 105 106 107 108 109 |
# File 'lib/savon_helper/mapping_object.rb', line 102 def to_json(*a) result = {} self.type_mappings.keys.each { |key| result[key] = self.send(key).to_json } { 'json_class' => self.class.name, 'data' => result }.to_json(*a) end |
#to_s ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/savon_helper/mapping_object.rb', line 20 def to_s public_vars = self.instance_variables.reject { |var| BLACK_LIST.include? var }.map { |var| "#{var}=\"#{instance_variable_get(var)}\"" }.join(" ") "<##{self.class}:#{self.object_id.to_s(8)} #{public_vars}>" end |
#to_savon ⇒ Hash
Return the instance as a hash of simple (type-converted) values suitable for Savon.
47 48 49 50 51 52 53 54 |
# File 'lib/savon_helper/mapping_object.rb', line 47 def to_savon hash = Hash.new() type_mappings.keys.each do |ivar_name| value = map_to_savon(ivar_name, instance_variable_get("@#{ivar_name}")) hash[ivar_name.to_sym] = value unless value.nil? end hash end |