Skip to content

Commit

Permalink
Use type definition when building type instance
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Sep 28, 2013
1 parent 61d484c commit 67489f0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions lib/virtus/attribute.rb
Expand Up @@ -59,8 +59,8 @@ def self.new(*args)
end

# @api private
def self.build_type(options)
Axiom::Types.infer(options[:primitive])
def self.build_type(definition)
Axiom::Types.infer(definition.primitive)
end

# @api private
Expand Down
46 changes: 23 additions & 23 deletions lib/virtus/attribute/builder.rb
Expand Up @@ -107,7 +107,7 @@ def initialize(type_definition, options)
@type_definition = type_definition

initialize_class
initialize_type(:type => type_definition.type, :primitive => type_definition.primitive)
initialize_type
initialize_options(options)
initialize_default_value
initialize_coercer
Expand All @@ -116,34 +116,14 @@ def initialize(type_definition, options)

private

# @api private
def initialize_attribute
@attribute = @klass.new(@type, @options) do |attribute|
attribute.extend(Accessor) if @options[:name]
attribute.extend(Coercible) if @options[:coerce]
attribute.extend(Strict) if @options[:strict]
attribute.extend(LazyDefault) if @options[:lazy]
end
end

# @api private
def initialize_default_value
@options.update(:default_value => DefaultValue.build(@options[:default]))
end

# @api private
def initialize_coercer
@options.update(:coercer => @options.fetch(:coercer) { @klass.build_coercer(@type, @options) })
end

# @api private
def initialize_class
@klass = self.class.determine_type(@type_definition.primitive, Attribute)
end

# @api private
def initialize_type(options)
@type = @klass.build_type(options)
def initialize_type
@type = @klass.build_type(@type_definition)
end

# @api private
Expand All @@ -161,6 +141,26 @@ def determine_visibility!
@options.update(:reader => reader_visibility, :writer => writer_visibility)
end

# @api private
def initialize_default_value
@options.update(:default_value => DefaultValue.build(@options[:default]))
end

# @api private
def initialize_coercer
@options.update(:coercer => @options.fetch(:coercer) { @klass.build_coercer(@type, @options) })
end

# @api private
def initialize_attribute
@attribute = @klass.new(@type, @options) do |attribute|
attribute.extend(Accessor) if @options[:name]
attribute.extend(Coercible) if @options[:coerce]
attribute.extend(Strict) if @options[:strict]
attribute.extend(LazyDefault) if @options[:lazy]
end
end

end # class Builder

end # class Attribute
Expand Down
4 changes: 2 additions & 2 deletions lib/virtus/attribute/collection.rb
Expand Up @@ -51,8 +51,8 @@ def coercion_method
end

# @api private
def self.build_type(options)
Type.infer(options.fetch(:type), options.fetch(:primitive))
def self.build_type(definition)
Type.infer(definition.type, definition.primitive)
end

# @api private
Expand Down
5 changes: 2 additions & 3 deletions lib/virtus/attribute/embedded_value.rb
Expand Up @@ -66,9 +66,8 @@ def self.handles?(klass)
end

# @api private
def self.build_type(options)
klass = options[:primitive].is_a?(Class) ? options[:primitive] : options[:type]

def self.build_type(definition)
klass = definition.primitive.is_a?(Class) ? definition.primitive : definition.type
Axiom::Types::Object.new { primitive klass }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/virtus/attribute/hash.rb
Expand Up @@ -58,8 +58,8 @@ def primitive
end

# @api private
def self.build_type(options)
Type.infer(options[:type])
def self.build_type(definition)
Type.infer(definition.type)
end

# @api private
Expand Down

0 comments on commit 67489f0

Please sign in to comment.