class Unparser::Anima

Original code before vendoring and reduction from: github.com/mbj/anima.

Attributes

attributes[R]

Return names

@return [AttributeSet]

Public Class Methods

new(*names) click to toggle source

Initialize object

@return [undefined]

rubocop:disable Lint/MissingSuper

# File lib/unparser/anima.rb, line 18
def initialize(*names)
  @attributes = names.uniq.map(&Attribute.public_method(:new)).freeze
end

Public Instance Methods

add(*names) click to toggle source

Return new anima with attributes added

@return [Anima]

@example

anima = Anima.new(:foo)
anima.add(:bar) # equals Anima.new(:foo, :bar)
# File lib/unparser/anima.rb, line 31
def add(*names)
  new(attribute_names + names)
end
attribute_names() click to toggle source

Return attribute names

@return [Enumerable<Symbol>]

# File lib/unparser/anima.rb, line 61
def attribute_names
  attributes.map(&:name)
end
attributes_hash(object) click to toggle source

Return attributes hash for instance

@param [Object] object

@return [Hash]

# File lib/unparser/anima.rb, line 52
def attributes_hash(object)
  attributes.each_with_object({}) do |attribute, attributes_hash|
    attributes_hash[attribute.name] = attribute.get(object)
  end
end
initialize_instance(object, attribute_hash) click to toggle source

Initialize instance

@param [Object] object

@param [Hash] attribute_hash

@return [self]

# File lib/unparser/anima.rb, line 73
def initialize_instance(object, attribute_hash)
  assert_known_attributes(object.class, attribute_hash)
  attributes.each do |attribute|
    attribute.load(object, attribute_hash)
  end
  self
end
remove(*names) click to toggle source

Return new anima with attributes removed

@return [Anima]

@example

anima = Anima.new(:foo, :bar)
anima.remove(:bar) # equals Anima.new(:foo)
# File lib/unparser/anima.rb, line 43
def remove(*names)
  new(attribute_names - names)
end

Private Instance Methods

assert_known_attributes(klass, attribute_hash) click to toggle source

Fail unless keys in attribute_hash matches attribute_names

@param [Class] klass

the class being initialized

@param [Hash] attribute_hash

the attributes to initialize +object+ with

@return [undefined]

@raise [Error]

# File lib/unparser/anima.rb, line 164
def assert_known_attributes(klass, attribute_hash)
  keys = attribute_hash.keys

  unknown = keys - attribute_names
  missing = attribute_names - keys

  unless unknown.empty? && missing.empty?
    fail Error.new(klass, missing, unknown)
  end
end
included(descendant) click to toggle source

Infect the instance with anima

@param [Class, Module] scope

@return [undefined]

# File lib/unparser/anima.rb, line 137
def included(descendant)
  descendant.instance_exec(self, attribute_names) do |anima, names|
    # Define anima method
    define_singleton_method(:anima) { anima }

    # Define instance methods
    include InstanceMethods

    # Define attribute readers
    attr_reader(*names)

    # Define equalizer
    include Equalizer.new(*names)
  end
end
new(attributes) click to toggle source

Return new instance

@param [Enumerable<Symbol>] attributes

@return [Anima]

# File lib/unparser/anima.rb, line 180
def new(attributes)
  self.class.new(*attributes)
end