Class: CinnamonSerial::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/cinnamon_serial/specification.rb

Overview

A Specification is a group of attribute mappings and custom code blocks to execute for a serializer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_map: {}, hydrate_blocks: []) ⇒ Specification

Returns a new instance of Specification.



16
17
18
19
# File 'lib/cinnamon_serial/specification.rb', line 16

def initialize(attribute_map: {}, hydrate_blocks: [])
  @attribute_map  = attribute_map
  @hydrate_blocks = hydrate_blocks
end

Instance Attribute Details

#attribute_mapObject (readonly)

Returns the value of attribute attribute_map.



14
15
16
# File 'lib/cinnamon_serial/specification.rb', line 14

def attribute_map
  @attribute_map
end

#hydrate_blocksObject (readonly)

Returns the value of attribute hydrate_blocks.



14
15
16
# File 'lib/cinnamon_serial/specification.rb', line 14

def hydrate_blocks
  @hydrate_blocks
end

Instance Method Details

#hydrate(block) ⇒ Object



34
35
36
37
38
# File 'lib/cinnamon_serial/specification.rb', line 34

def hydrate(block)
  @hydrate_blocks << block

  nil
end

#set(*keys) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cinnamon_serial/specification.rb', line 21

def set(*keys)
  keys = keys.flatten

  # We have been sent options
  options = Resolver.new(keys.last.is_a?(Hash) ? keys.pop : {})

  raise ArgumentError, 'keys cannot be empty' if keys.empty?

  keys.each { |key| @attribute_map[key.to_s] = options }

  nil
end