Module: SaxStream::Mapper::ClassMethods
- Defined in:
- lib/sax_stream/mapper.rb
Instance Method Summary collapse
- #attribute_group(group_name) ⇒ Object
- #child_handler_for(key, collector, handler_stack, current_object) ⇒ 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) ⇒ Object
- #mappings ⇒ Object
- #maps_node?(name) ⇒ Boolean
- #node(name, options = {}) ⇒ Object
- #node_name ⇒ Object
- #regex_mappings ⇒ 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
24 25 26 27 28 29 |
# File 'lib/sax_stream/mapper.rb', line 24 def attribute_group(group_name) self. = {group_name: group_name} yield ensure end |
#child_handler_for(key, collector, handler_stack, current_object) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/sax_stream/mapper.rb', line 96 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 |
#group_keys(group_name) ⇒ Object
121 122 123 124 |
# File 'lib/sax_stream/mapper.rb', line 121 def group_keys(group_name) @group_keys ||= {} @group_keys[group_name] ||= [] end |
#map(attribute_name, options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/sax_stream/mapper.rb', line 18 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
76 77 78 |
# File 'lib/sax_stream/mapper.rb', line 76 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
80 81 82 83 84 85 |
# File 'lib/sax_stream/mapper.rb', line 80 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
87 88 89 90 91 92 93 94 |
# File 'lib/sax_stream/mapper.rb', line 87 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 |
#mappings ⇒ Object
107 108 109 |
# File 'lib/sax_stream/mapper.rb', line 107 def mappings @mappings_incuding_inherited ||= parent_class_values(:mappings, CoreExtensions::OrderedHash.new).merge(class_mappings).freeze end |
#maps_node?(name) ⇒ Boolean
72 73 74 |
# File 'lib/sax_stream/mapper.rb', line 72 def maps_node?(name) @node_name == name || @node_name == '*' end |
#node(name, options = {}) ⇒ Object
13 14 15 16 |
# File 'lib/sax_stream/mapper.rb', line 13 def node(name, = {}) @node_name = name @collect = .has_key?(:collect) ? [:collect] : true end |
#node_name ⇒ Object
68 69 70 |
# File 'lib/sax_stream/mapper.rb', line 68 def node_name @node_name end |
#regex_mappings ⇒ Object
111 112 113 114 115 |
# File 'lib/sax_stream/mapper.rb', line 111 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. 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.
63 64 65 66 |
# File 'lib/sax_stream/mapper.rb', line 63 def relate(attribute_name, = {}) [:to] ||= '*' store_relation_mapping([:to], Internal::MappingFactory.build_relation(attribute_name, )) end |
#relation_mappings ⇒ Object
103 104 105 |
# File 'lib/sax_stream/mapper.rb', line 103 def relation_mappings (class_relation_mappings + parent_class_values(:relation_mappings, [])).freeze end |
#should_collect? ⇒ Boolean
117 118 119 |
# File 'lib/sax_stream/mapper.rb', line 117 def should_collect? @collect end |