class Unparser::Anima
Original code before vendoring and reduction from: github.com/mbj/anima.
Attributes
Return names
@return [AttributeSet]
Public Class Methods
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
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
Return attribute names
@return [Enumerable<Symbol>]
# File lib/unparser/anima.rb, line 61 def attribute_names attributes.map(&:name) end
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
@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
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
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
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
Return new instance
@param [Enumerable<Symbol>] attributes
@return [Anima]
# File lib/unparser/anima.rb, line 180 def new(attributes) self.class.new(*attributes) end