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.
-
#name ⇒ Object
Returns the value of attribute name.
-
#no_keys ⇒ Object
Returns the value of attribute no_keys.
-
#type(val = nil) ⇒ Object
Returns the value of attribute type.
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.
33 34 35 36 |
# File 'lib/jsonapi_spec_helpers/payload.rb', line 33 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 |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/jsonapi_spec_helpers/payload.rb', line 8 def name @name 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 |
#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
#fork ⇒ Object
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) = 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
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 @keys[:created_at] = key(:created_at, String) @keys[:updated_at] = key(:updated_at, String) end |