Module: SaxStream::Mapper::ClassMethods

Defined in:
lib/sax_stream/mapper.rb

Instance Method Summary collapse

Instance Method Details

#child_handler_for(key, collector, handler_stack, current_object) ⇒ Object



85
86
87
88
89
90
# File 'lib/sax_stream/mapper.rb', line 85

def child_handler_for(key, collector, handler_stack, current_object)
  mapping = field_mapping(key)
  if mapping
    mapping.handler_for(key, collector, handler_stack, current_object)
  end
end

#map(attribute_name, options = {}) ⇒ Object



18
19
20
# File 'lib/sax_stream/mapper.rb', line 18

def map(attribute_name, options = {})
  store_field_mapping(options[:to], Internal::MappingFactory.build_mapping(attribute_name, options))
end

#map_attribute_onto_object(object, key, value) ⇒ Object



65
66
67
# File 'lib/sax_stream/mapper.rb', line 65

def map_attribute_onto_object(object, key, value)
  map_key_onto_object(object, "@#{key}", value)
end

#map_element_stack_top_onto_object(object, element_stack) ⇒ Object



69
70
71
72
73
74
# File 'lib/sax_stream/mapper.rb', line 69

def map_element_stack_top_onto_object(object, element_stack)
  map_key_onto_object(object, element_stack.path, element_stack.content)
  element_stack.attributes.each do |key, value|
    map_key_onto_object(object, key, value)
  end
end

#map_key_onto_object(object, key, value) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/sax_stream/mapper.rb', line 76

def map_key_onto_object(object, key, value)
  if value
    mapping = field_mapping(key)
    if mapping
      mapping.map_value_onto_object(object, value)
    end
  end
end

#mappingsObject



96
97
98
# File 'lib/sax_stream/mapper.rb', line 96

def mappings
  @mappings_incuding_inherited ||= parent_class_values(:mappings, CoreExtensions::OrderedHash.new).merge(class_mappings).freeze
end

#maps_node?(name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sax_stream/mapper.rb', line 61

def maps_node?(name)
  @node_name == name
end

#node(name, options = {}) ⇒ Object



13
14
15
16
# File 'lib/sax_stream/mapper.rb', line 13

def node(name, options = {})
  @node_name = name
  @collect = options.has_key?(:collect) ? options[:collect] : true
end

#node_nameObject



57
58
59
# File 'lib/sax_stream/mapper.rb', line 57

def node_name
  @node_name
end

#regex_mappingsObject



100
101
102
103
104
# File 'lib/sax_stream/mapper.rb', line 100

def regex_mappings
  @regex_mappings ||= mappings.reject do |key, mapping|
    !key.is_a?(Regexp)
  end
end

#relate(attribute_name, options = {}) ⇒ Object

Define a relation to another object which is built from an XML node using another class which also includes SaxStream::Mapper.

attribute_name

The name of the attribute on your object that the related objects will be stored in.

options

An options hash which can accept the following-

[:to] Default value: "*"
      The path to the XML which defines an instance of this related node. This
      is a bit like an XPath expression, but not quite. See the README for examples.
      For relations, this can include a wildcard "*" as the last part of the path,
      eg: "product/review/*". If the path is just set to "*" then this will match
      any immediate child of the current node, which is good for polymorphic collections.
[:as] Required, no default value.
      Needs to be a class which includes SaxStream::Mapper, or an array of such classes.
      Using an array of classes, even if the array only has one item, denotes that an
      array of related items are expected. Calling @object.relations['name'] will return
      an array which will be empty if nothing is found. If a singular value is used for
      the :as option, then the relation will be assumed to be singular, and so it will
      be nil or the expected class (and will raise an error if multiples are in the file).
[:parent_collects] Default value: false
      Set to true if the object defining this relationship (ie, the parent
      in the relationship) needs to collect the defined children. If so, the
      parent object will be used as the collector for these children, and they
      will not be passed to the collector supplied to the parser. Use this when
      the child objects are not something you want to process on their own, but
      instead you want them all to be loaded into the parent which will then be
      collected as normal. If this is left false, then the parent ojbect will
      not be informed of it's children, because they will be passed to the collector
      and then forgotten about. However, the children will know about their parent,
      or at least what is known about it, but the parent will not be finished being
      parsed. The parent will have already parsed all XML attributes though.


52
53
54
55
# File 'lib/sax_stream/mapper.rb', line 52

def relate(attribute_name, options = {})
  options[:to] ||= '*'
  store_relation_mapping(options[:to], Internal::MappingFactory.build_relation(attribute_name, options))
end

#relation_mappingsObject



92
93
94
# File 'lib/sax_stream/mapper.rb', line 92

def relation_mappings
  (class_relation_mappings + parent_class_values(:relation_mappings, [])).freeze
end

#should_collect?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/sax_stream/mapper.rb', line 106

def should_collect?
  @collect
end