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.



23
24
25
26
# File 'lib/jsonapi_spec_helpers/payload.rb', line 23

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

#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

Class Method Details

.register(name, &blk) ⇒ Object



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

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

Instance Method Details

#forkObject



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

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

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



33
34
35
36
37
38
39
40
41
# File 'lib/jsonapi_spec_helpers/payload.rb', line 33

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



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

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

#timestamps!Object



43
44
45
46
# File 'lib/jsonapi_spec_helpers/payload.rb', line 43

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