lib/koine/attributes.rb in koine-attributes-0.1.4 vs lib/koine/attributes.rb in koine-attributes-0.2.0
- old
+ new
@@ -1,7 +1,7 @@
+require 'forwardable'
require 'koine/attributes/version'
-require 'koine/attributes/builder'
require 'koine/attributes/adapter/base'
# provides the following API
#
# @example using attributes
@@ -99,10 +99,11 @@
# new_location = location.with_lon(3)
#
module Koine
module Attributes
autoload :Attributes, 'koine/attributes/attributes'
+ autoload :AttributesFactory, 'koine/attributes/attributes_factory'
module Adapter
autoload :Boolean, 'koine/attributes/adapter/boolean'
autoload :Date, 'koine/attributes/adapter/date'
autoload :Time, 'koine/attributes/adapter/time'
@@ -112,45 +113,47 @@
end
Error = Class.new(StandardError)
def self.included(base)
+ base.extend(Forwardable)
base.extend(ClassMethods)
end
module ClassMethods
def attributes(options = {}, &block)
- @builder = Builder::ClassBuilder.new(target: self)
+ @builder = true
+ @_attributes_factory ||= AttributesFactory.new(options)
class_eval(&block)
- if options[:initializer]
- initializer_options = options[:initializer]
+ instance_eval do
+ define_method :attributes do
+ @_attributes ||= self.class.instance_variable_get(:@_attributes_factory).create(self)
+ end
- initializer_options = {} unless initializer_options.is_a?(Hash)
-
- @builder.build_constructor(initializer_options)
- else
- @builder.build_lazy_attributes
+ define_method(:initialize) { |*args| attributes.initialize_values(*args) }
end
+ @_attributes_factory.freeze
+
@builder = nil
end
- def attribute(name, adapter, lambda_constructor = nil, &block)
+ def attribute(name, adapter, lambda_arg = nil, &block)
unless @builder
raise Error, 'You must call .attribute inside the .attributes block'
end
- adapter = coerce_adapter(adapter, lambda_constructor, &block)
- @builder.build(name, adapter)
- end
+ block = lambda_arg || block
+ instance_variable_get(:@_attributes_factory).add_attribute(name, adapter, &block)
- private def coerce_adapter(adapter, lambda_constructor, &block)
- return adapter unless adapter.instance_of?(::Symbol)
- adapter = const_get("Koine::Attributes::Adapter::#{adapter.to_s.capitalize}").new
- lambda_constructor.call(adapter) if lambda_constructor
- yield(adapter) if block
- adapter
+ instance_eval do
+ def_delegators :attributes, name, "#{name}=", "with_#{name}"
+
+ define_method :== do |other|
+ attributes == other.attributes
+ end
+ end
end
end
end
end