Class: JsonapiSpecHelpers::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_spec_helpers/payload.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePayload

Returns a new instance of Payload.



33
34
35
36
# File 'lib/jsonapi_spec_helpers/payload.rb', line 33

def initialize
  @keys = {}
  @no_keys = []
end

Class Attribute Details

.registryObject

Returns the value of attribute registry.



4
5
6
# File 'lib/jsonapi_spec_helpers/payload.rb', line 4

def registry
  @registry
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



8
9
10
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8

def keys
  @keys
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8

def name
  @name
end

#no_keysObject

Returns the value of attribute no_keys.



8
9
10
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8

def no_keys
  @no_keys
end

#type(val = nil) ⇒ Object

Returns the value of attribute type.



8
9
10
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8

def type
  @type
end

Class Method Details

.by_type(type) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/jsonapi_spec_helpers/payload.rb', line 17

def self.by_type(type)
  found = nil
  registry.each_pair do |name, payload|
    found = payload if payload.type == type
  end
  raise "Could not find payload for type #{type}" unless found
  found
end

.register(name, &blk) ⇒ Object



10
11
12
13
14
15
# File 'lib/jsonapi_spec_helpers/payload.rb', line 10

def self.register(name, &blk)
  instance = new
  instance.instance_eval(&blk)
  instance.name = name
  registry[name] = instance
end

Instance Method Details

#forkObject



26
27
28
29
30
31
# File 'lib/jsonapi_spec_helpers/payload.rb', line 26

def fork
  instance = self.class.new
  instance.keys = keys.dup
  instance.no_keys = no_keys.dup
  instance
end

#key(name, *args, &blk) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/jsonapi_spec_helpers/payload.rb', line 51

def key(name, *args, &blk)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:type] = args.first
  options[:allow_nil] ||= false
  @no_keys.reject! { |k| k == name }
  prc = blk
  prc = ->(record) { record.send(name) } if prc.nil?
  @keys[name] = options.merge(proc: prc)
end

#no_key(name) ⇒ Object



38
39
40
41
# File 'lib/jsonapi_spec_helpers/payload.rb', line 38

def no_key(name)
  @keys.delete(name)
  @no_keys << name
end

#timestamps!Object



61
62
63
64
# File 'lib/jsonapi_spec_helpers/payload.rb', line 61

def timestamps!
  @keys[:created_at] = key(:created_at, String)
  @keys[:updated_at] = key(:updated_at, String)
end