Class: Serial::HashBuilder
- Defined in:
- lib/serial/hash_builder.rb
Overview
A builder for building hashes. You most likely just want to look at the public API methods in this class.
Instance Attribute Summary
Attributes inherited from Builder
Instance Method Summary collapse
-
#attribute(key, value = nil) {|builder, value| ... } ⇒ Object
Declare an attribute.
-
#attribute!(key, value = nil) {|builder, value| ... } ⇒ Object
Same as #attribute, but will not raise an error on duplicate keys.
-
#collection(key) {|builder| ... } ⇒ Object
Declare a collection attribute.
-
#collection!(key) {|builder| ... } ⇒ Object
Same as #collection, but will not raise an error on duplicate keys.
-
#initialize(context) ⇒ HashBuilder
constructor
private
A new instance of HashBuilder.
-
#map(key, list) {|builder, value| ... } ⇒ Object
Declare a collection attribute from a list of values.
-
#map!(key, list) {|builder, value| ... } ⇒ Object
Same as #map, but will not raise an error on duplicate keys.
-
#merge(value) {|builder, value| ... } ⇒ Object
Merge another serializer into the current serialization.
-
#merge!(value) {|builder, value| ... } ⇒ Object
Same as #merge, but will not raise an error on duplicate keys.
Methods inherited from Builder
Constructor Details
permalink #initialize(context) ⇒ HashBuilder
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of HashBuilder.
6 7 8 9 |
# File 'lib/serial/hash_builder.rb', line 6 def initialize(context) @context = context @data = {} end |
Instance Method Details
permalink #attribute(key, value = nil) {|builder, value| ... } ⇒ Object
Declare an attribute.
28 29 30 31 |
# File 'lib/serial/hash_builder.rb', line 28 def attribute(key, value = nil, &block) check_duplicate_key!(key) attribute!(key, value, &block) end |
permalink #attribute!(key, value = nil) {|builder, value| ... } ⇒ Object
Same as #attribute, but will not raise an error on duplicate keys.
40 41 42 43 |
# File 'lib/serial/hash_builder.rb', line 40 def attribute!(key, value = nil, &block) value = HashBuilder.build(@context, value, &block) if block @data[key.to_s] = value end |
permalink #collection(key) {|builder| ... } ⇒ Object
Declare a collection attribute. This is a low-level method, see #map instead.
67 68 69 70 |
# File 'lib/serial/hash_builder.rb', line 67 def collection(key, &block) check_duplicate_key!(key) collection!(key, &block) end |
permalink #collection!(key) {|builder| ... } ⇒ Object
Same as #collection, but will not raise an error on duplicate keys.
79 80 81 |
# File 'lib/serial/hash_builder.rb', line 79 def collection!(key, &block) attribute!(key, ArrayBuilder.build(@context, &block)) end |
permalink #map(key, list) {|builder, value| ... } ⇒ Object
Declare a collection attribute from a list of values.
97 98 99 100 |
# File 'lib/serial/hash_builder.rb', line 97 def map(key, list, &block) check_duplicate_key!(key) map!(key, list, &block) end |
permalink #map!(key, list) {|builder, value| ... } ⇒ Object
Same as #map, but will not raise an error on duplicate keys.
109 110 111 112 113 114 115 116 117 |
# File 'lib/serial/hash_builder.rb', line 109 def map!(key, list, &block) collection!(key) do |builder| list.each do |item| builder.element do |element| element.exec(item, &block) end end end end |
permalink #merge(value) {|builder, value| ... } ⇒ Object
Merge another serializer into the current serialization.
133 134 135 136 137 |
# File 'lib/serial/hash_builder.rb', line 133 def merge(value, &serializer) hash = HashBuilder.build(@context, value, &serializer) hash.keys.each { |key| check_duplicate_key!(key) } @data.merge!(hash) end |
permalink #merge!(value) {|builder, value| ... } ⇒ Object
Same as #merge, but will not raise an error on duplicate keys.
146 147 148 149 |
# File 'lib/serial/hash_builder.rb', line 146 def merge!(value, &serializer) hash = HashBuilder.build(@context, value, &serializer) @data.merge!(hash) end |