Class: Unparser::Anima

Inherits:
Module
  • Object
show all
Includes:
Adamantium
Defined in:
lib/unparser/anima.rb,
lib/unparser/anima/error.rb,
lib/unparser/anima/attribute.rb

Overview

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

Defined Under Namespace

Modules: InstanceMethods Classes: Attribute, Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ undefined

Initialize object

rubocop:disable Lint/MissingSuper



18
19
20
# File 'lib/unparser/anima.rb', line 18

def initialize(*names)
  @attributes = names.uniq.map(&Attribute.public_method(:new)).freeze
end

Instance Attribute Details

#attributesAttributeSet (readonly)

Return names

Returns:

  • (AttributeSet)


11
12
13
# File 'lib/unparser/anima.rb', line 11

def attributes
  @attributes
end

Instance Method Details

#add(*names) ⇒ Anima

Return new anima with attributes added

Examples:

anima = Anima.new(:foo)
anima.add(:bar) # equals Anima.new(:foo, :bar)

Returns:



31
32
33
# File 'lib/unparser/anima.rb', line 31

def add(*names)
  new(attribute_names + names)
end

#attribute_namesEnumerable<Symbol>

Return attribute names

Returns:

  • (Enumerable<Symbol>)


61
62
63
# File 'lib/unparser/anima.rb', line 61

def attribute_names
  attributes.map(&:name)
end

#attributes_hash(object) ⇒ Hash

Return attributes hash for instance

Parameters:

  • object (Object)

Returns:

  • (Hash)


52
53
54
55
56
# 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) ⇒ self

Initialize instance

Parameters:

  • object (Object)
  • attribute_hash (Hash)

Returns:

  • (self)


73
74
75
76
77
78
79
# 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) ⇒ Anima

Return new anima with attributes removed

Examples:

anima = Anima.new(:foo, :bar)
anima.remove(:bar) # equals Anima.new(:foo)

Returns:



43
44
45
# File 'lib/unparser/anima.rb', line 43

def remove(*names)
  new(attribute_names - names)
end