class Anima
Main library namespace and mixin @api private
Attributes
attributes[R]
Return names
@return [AttributeSet]
Public Class Methods
new(*names)
click to toggle source
Initialize object
@return [undefined]
# File lib/anima.rb, line 18 def initialize(*names) @attributes = names.uniq.map(&Attribute.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/anima.rb, line 30 def add(*names) new(attribute_names + names) end
attribute_names()
click to toggle source
Return attribute names
@return [Enumerable<Symbol>]
# File lib/anima.rb, line 60 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/anima.rb, line 51 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/anima.rb, line 72 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/anima.rb, line 42 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/anima.rb, line 160 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/anima.rb, line 133 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/anima.rb, line 176 def new(attributes) self.class.new(*attributes) end