module Koine::Attributes::ClassMethods

Private Instance Methods

array_of(item_adapter) click to toggle source
# File lib/koine/attributes.rb, line 180
def array_of(item_adapter)
  adapter = @_attributes_factory.coerce_adapter(item_adapter)
  Adapter::ArrayOf.new(adapter)
end
attribute(name, adapter, lambda_arg = nil, &block) click to toggle source
# File lib/koine/attributes.rb, line 153
def attribute(name, adapter, lambda_arg = nil, &block)
  unless @builder
    raise Error, 'You must call .attribute inside the .attributes block'
  end

  block = lambda_arg || block
  @_attributes_factory.add_attribute(name, adapter, &block)

  instance_eval do
    define_method name do
      attributes.send(name)
    end

    define_method "#{name}=" do |value|
      attributes.send("#{name}=", value)
    end

    define_method "with_#{name}" do |value|
      attributes.send("with_#{name}", value)
    end

    define_method :== do |other|
      attributes == other.send(:attributes)
    end
  end
end
attributes(options = {}, &block) click to toggle source
# File lib/koine/attributes.rb, line 128
def attributes(options = {}, &block)
  @builder = true
  @_attributes_factory ||= AttributesFactory.new(options)
  class_eval(&block)

  instance_eval do
    define_method :attributes do
      @_attributes ||= self.class.instance_variable_get(:@_attributes_factory).create(self)
    end

    private :attributes

    define_method(:initialize) { |*args| attributes.initialize_values(*args) }

    define_method(:inspect) do
      string = Object.instance_method(:inspect).bind(self).call.split(':')[1].split(' ').first
      "#<#{self.class}:#{string} @attributes=#{attributes.to_h.inspect}>"
    end
  end

  @_attributes_factory.freeze

  @builder = nil
end
hash_of(key_adapter, value_adapter) click to toggle source
# File lib/koine/attributes.rb, line 185
def hash_of(key_adapter, value_adapter)
  key_adapter = @_attributes_factory.coerce_adapter(key_adapter)
  value_adapter = @_attributes_factory.coerce_adapter(value_adapter)
  Adapter::HashOf.new(key_adapter, value_adapter)
end