class Morpher::Record

Generator for struct a-like wrappers

Constants

DEFAULTS

Public Class Methods

new(**attributes) click to toggle source
Calls superclass method
# File lib/morpher/record.rb, line 13
def self.new(**attributes)
  super(DEFAULTS.merge(attributes))
end

Public Instance Methods

included(host) click to toggle source

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

# File lib/morpher/record.rb, line 19
def included(host)
  optional           = optional()
  optional_transform = transform(optional)
  required           = required()
  required_transform = transform(required)

  host.class_eval do
    include Adamantium::Flat, Anima.new(*(required.keys + optional.keys))

    const_set(
      :TRANSFORM,
      Transform::Sequence.new(
        [
          Transform::Primitive.new(Hash),
          Transform::Hash::Symbolize.new,
          Transform::Hash.new(
            required: required_transform,
            optional: optional_transform
          ),
          Transform::Success.new(public_method(:new))
        ]
      )
    )
  end
end

Private Instance Methods

transform(attributes) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/morpher/record.rb, line 49
def transform(attributes)
  attributes.map do |name, transform|
    Transform::Hash::Key.new(name, transform)
  end
end