Class: Serial::HashBuilder

Inherits:
Builder
  • Object
show all
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

#data

Instance Method Summary collapse

Methods inherited from Builder

build, #exec

Constructor Details

#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

#attribute(key, value = nil) {|builder, value| ... } ⇒ Object

Declare an attribute.

Examples:

without block

h.attribute(:id, 5) # => { "id" => 5 }

nested attribute, with block

h.attribute(:project, project) do |h, project|
  h.attribute(:name, project.name)
end # => { "project" => { "name" => … } }

Parameters:

  • key (#to_s)
  • value (defaults to: nil)

Yields:

  • (builder, value)

    declare nested attribute if block is given

Yield Parameters:

  • builder (HashBuilder)

    (keep in mind the examples shadow the outer ‘h` variable)

  • value

Raises:



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

#attribute!(key, value = nil) {|builder, value| ... } ⇒ Object

Same as #attribute, but will not raise an error on duplicate keys.

Parameters:

  • key (#to_s)
  • value (defaults to: nil)

Yields:

  • (builder, value)

    declare nested attribute if block is given

Yield Parameters:

  • builder (HashBuilder)

    (keep in mind the examples shadow the outer ‘h` variable)

  • value

See Also:



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

#collection(key) {|builder| ... } ⇒ Object

Declare a collection attribute. This is a low-level method, see #map instead.

Examples:

h.collection(:people) do |l|
  l.element do |h|
    h.attribute()
  end
  l.element do |h|
    h.attribute()
  end
  l.collection do |l|
    l.element do |h|
      h.attribute()
    end
  end
end # => { "people" => [{…}, {…}, [{…}]] }

Parameters:

  • key (#to_s)

Yields:

  • (builder)

Yield Parameters:

See Also:



67
68
69
70
# File 'lib/serial/hash_builder.rb', line 67

def collection(key, &block)
  check_duplicate_key!(key)
  collection!(key, &block)
end

#collection!(key) {|builder| ... } ⇒ Object

Same as #collection, but will not raise an error on duplicate keys.

Parameters:

  • key (#to_s)

Yields:

  • (builder)

Yield Parameters:

See Also:



79
80
81
# File 'lib/serial/hash_builder.rb', line 79

def collection!(key, &block)
  attribute!(key, ArrayBuilder.build(@context, &block))
end

#map(key, list) {|builder, value| ... } ⇒ Object

Declare a collection attribute from a list of values.

Examples:

h.map(:people, project.people) do |h, person|
  h.attribute(:name, person.name)
end # => { "people" => [{ "name" => … }] }

Parameters:

  • key (#to_s)
  • list (#each)

Yields:

  • (builder, value)

    yields each value from list to build an array of hashes

Yield Parameters:

See Also:



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

#map!(key, list) {|builder, value| ... } ⇒ Object

Same as #map, but will not raise an error on duplicate keys.

Parameters:

  • key (#to_s)
  • list (#each)

Yields:

  • (builder, value)

    yields each value from list to build an array of hashes

Yield Parameters:

See Also:



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

#merge(value) {|builder, value| ... } ⇒ Object

Merge another serializer into the current serialization.

Examples:

ExtendedProjectSerializer = Serial::Serializer.new do |h, project|
  h.merge(project, &ProjectSerializer)
  h.attribute(:extra, project.extra_info)
end # => { "name" => …, …, "extra" => … }

Parameters:

  • value

Yields:

  • (builder, value)

    to another serializer

Yield Parameters:

Raises:



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

#merge!(value) {|builder, value| ... } ⇒ Object

Same as #merge, but will not raise an error on duplicate keys.

Parameters:

  • value

Yields:

  • (builder, value)

    to another serializer

Yield Parameters:

See Also:



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