Class: Alchemrest::Data::Record

Inherits:
Module
  • Object
show all
Defined in:
lib/alchemrest/data/record.rb

Direct Known Subclasses

Schema

Constant Summary collapse

DEFAULTS =
{
  required: Morpher::EMPTY_HASH,
  optional: Morpher::EMPTY_HASH,
  allow_additional_properties: true,
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(**attributes) ⇒ Object



14
15
16
# File 'lib/alchemrest/data/record.rb', line 14

def self.new(**attributes)
  super(**DEFAULTS.merge(attributes))
end

Instance Method Details

#included(host) ⇒ Object

rubocop:disable Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/alchemrest/data/record.rb', line 19

def included(host)
  optional           = optional()
  optional_transform = transform(optional)
  required           = required()
  required_transform = transform(required)
  additional_properties_allowed = allow_additional_properties

  host.class_eval do
    include Anima.new(*(required.keys + optional.keys))

    const_set(
      :TRANSFORM,
      Transforms::Typed.new(
        transform: Morpher::Transform::Sequence.new(
          [
            Morpher::Transform::Primitive.new(Hash),
            Morpher::Transform::Hash::Symbolize.new,
            Transforms::LooseHash.new(
              required: required_transform,
              optional: optional_transform,
              allow_additional_properties: additional_properties_allowed,
            ),
            Morpher::Transform::Success.new(public_method(:new)),
          ],
        ),
        output_type: Transforms::OutputType.simple(host),
      ),
    )
  end
end