Module: SaxStream::Mapper::ClassMethods
- Defined in:
- lib/sax_stream/mapper.rb
Instance Method Summary collapse
- #attribute_group(group_name) ⇒ Object
- #child_handler_for(key, attributes, collector, handler_stack, current_object) ⇒ Object
- #field_mapping(key, attributes = []) ⇒ Object
- #group_keys(group_name) ⇒ Object
- #map(attribute_name, options = {}) ⇒ Object
- #map_attribute_onto_object(object, key, value) ⇒ Object
- #map_element_stack_top_onto_object(object, element_stack) ⇒ Object
- #map_key_onto_object(object, key, value, attributes = []) ⇒ Object
- #mappings ⇒ Object
- #maps_node?(name) ⇒ Boolean
- #node(name, options = {}) ⇒ Object
- #node_name ⇒ Object
-
#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.
- #relation_mappings ⇒ Object
- #should_collect? ⇒ Boolean
Instance Method Details
#attribute_group(group_name) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/sax_stream/mapper.rb', line 25 def attribute_group(group_name) self. = {group_name: group_name} yield ensure end |
#child_handler_for(key, attributes, collector, handler_stack, current_object) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/sax_stream/mapper.rb', line 97 def child_handler_for(key, attributes, collector, handler_stack, current_object) mapping = field_mapping(key, attributes) if mapping mapping.handler_for(key, collector, handler_stack, current_object) end end |
#field_mapping(key, attributes = []) ⇒ Object
121 122 123 |
# File 'lib/sax_stream/mapper.rb', line 121 def field_mapping(key, attributes = []) field_mappings.field_mapping(key, attributes) || parent_field_mapping(key, attributes) end |
#group_keys(group_name) ⇒ Object
116 117 118 119 |
# File 'lib/sax_stream/mapper.rb', line 116 def group_keys(group_name) @group_keys ||= {} @group_keys[group_name] ||= [] end |
#map(attribute_name, options = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/sax_stream/mapper.rb', line 19 def map(attribute_name, = {}) store_key_for_group attribute_name mapping = Internal::MappingFactory.build_mapping(attribute_name, ) store_field_mapping([:to], mapping) end |
#map_attribute_onto_object(object, key, value) ⇒ Object
77 78 79 |
# File 'lib/sax_stream/mapper.rb', line 77 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
81 82 83 84 85 86 |
# File 'lib/sax_stream/mapper.rb', line 81 def map_element_stack_top_onto_object(object, element_stack) map_key_onto_object(object, element_stack.path, element_stack.content, element_stack.relative_attributes) element_stack.attributes.each do |key, value| map_key_onto_object(object, key, value) end end |
#map_key_onto_object(object, key, value, attributes = []) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/sax_stream/mapper.rb', line 88 def map_key_onto_object(object, key, value, attributes = []) if value mapping = field_mapping(key, attributes) if mapping mapping.map_value_onto_object(object, value) end end end |
#mappings ⇒ Object
108 109 110 |
# File 'lib/sax_stream/mapper.rb', line 108 def mappings @mappings_incuding_inherited ||= parent_class_values(:mappings, CoreExtensions::OrderedHash.new).merge(class_mappings).freeze end |
#maps_node?(name) ⇒ Boolean
73 74 75 |
# File 'lib/sax_stream/mapper.rb', line 73 def maps_node?(name) @node_name == name || @node_name == '*' end |
#node(name, options = {}) ⇒ Object
14 15 16 17 |
# File 'lib/sax_stream/mapper.rb', line 14 def node(name, = {}) @node_name = name @collect = .has_key?(:collect) ? [:collect] : true end |
#node_name ⇒ Object
69 70 71 |
# File 'lib/sax_stream/mapper.rb', line 69 def node_name @node_name 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. You can also specify an array of strings to match against multiple paths, for example, to: ['/images/*', 'files/*', 'base_image'] [: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.
64 65 66 67 |
# File 'lib/sax_stream/mapper.rb', line 64 def relate(attribute_name, = {}) [:to] ||= '*' store_relation_mapping([:to], Internal::MappingFactory.build_relation(attribute_name, )) end |
#relation_mappings ⇒ Object
104 105 106 |
# File 'lib/sax_stream/mapper.rb', line 104 def relation_mappings (class_relation_mappings + parent_class_values(:relation_mappings, [])).freeze end |
#should_collect? ⇒ Boolean
112 113 114 |
# File 'lib/sax_stream/mapper.rb', line 112 def should_collect? @collect end |