Module: JSONAPIonify::Structure::Helpers::ObjectDefaults::ClassMethods

Defined in:
lib/jsonapionify/structure/helpers/object_defaults.rb

Instance Method Summary collapse

Instance Method Details

#collects(key, as:, **opts) ⇒ Object

Forces the setter to the collection type Allows unset types to expose collection and type_map methods



19
20
21
22
# File 'lib/jsonapionify/structure/helpers/object_defaults.rb', line 19

def collects(key, as:, **opts)
  type_of! key, must_be: as
  track_collection key, as, **opts
end

#collects_or_implements(key, implements:, collects:, allow_nil: false, **opts) ⇒ Object Also known as: implements_or_collects

implements_or_collects(:data, implements: A, collects: B, if: ->(obj){ obj.has_key? :attributes }) implements_or_collects(:data, implements: C, collects: D, unless: ->(obj){ obj.has_key? :attributes })



26
27
28
29
30
31
32
# File 'lib/jsonapionify/structure/helpers/object_defaults.rb', line 26

def collects_or_implements(key, implements:, collects:, allow_nil: false, **opts)
  allowed = [implements, collects]
  allowed << NilClass if allow_nil
  type_of! key, must_be: allowed
  track_collection key, collects, **opts
  track_implementation key, implements, **opts
end

#default(key, &block) ⇒ Object

Defaults



37
38
39
40
41
# File 'lib/jsonapionify/structure/helpers/object_defaults.rb', line 37

def default(key, &block)
  after_initialize do
    self[key] ||= instance_eval(&block)
  end
end

#implements(key, as:, **opts) ⇒ Object

Forces the setter to a type



12
13
14
15
# File 'lib/jsonapionify/structure/helpers/object_defaults.rb', line 12

def implements(key, as:, **opts)
  type_of! key, must_be: as, allow_nil: true
  track_implementation key, as, **opts
end