Class: JsonapiSpecHelpers::Payload
- Inherits:
-
Object
- Object
- JsonapiSpecHelpers::Payload
- Defined in:
- lib/jsonapi_spec_helpers/payload.rb
Class Attribute Summary collapse
-
.registry ⇒ Object
Returns the value of attribute registry.
Instance Attribute Summary collapse
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#no_keys ⇒ Object
Returns the value of attribute no_keys.
Class Method Summary collapse
Instance Method Summary collapse
- #fork ⇒ Object
-
#initialize ⇒ Payload
constructor
A new instance of Payload.
- #key(name, *args, &blk) ⇒ Object
- #no_key(name) ⇒ Object
- #timestamps! ⇒ Object
Constructor Details
#initialize ⇒ Payload
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
.registry ⇒ Object
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
#keys ⇒ Object
Returns the value of attribute keys.
8 9 10 |
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8 def keys @keys end |
#no_keys ⇒ Object
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
#fork ⇒ Object
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) = args.last.is_a?(Hash) ? args.pop : {} [:type] = args.first [:allow_nil] ||= false @no_keys.reject! { |k| k == name } prc = blk prc = ->(record) { record.send(name) } if prc.nil? @keys[name] = .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 @keys[:created_at] = key(:created_at) @keys[:updated_at] = key(:updated_at) end |