Permalink
Browse files

Moved Virtus.determine_type to Virtus::Attribute.determine_type

* Everything the determine_type depends on is an Attribute concern, so I moved
  it to the class that contains the responsibility of modelling Attributes.
* Made other Attribute.determine_type_* methods private since they are just
  helper methods and are not intended to be used in isolation.
  • Loading branch information...
1 parent 880a097 commit 3b0df3baf663d9d02ba9039576179a69f43c3940 @dkubb dkubb committed Jul 8, 2011
View
@@ -1,3 +1,3 @@
---
threshold: 19.0
-total_score: 201
+total_score: 198
View
@@ -2,7 +2,7 @@
AbcMetricMethodCheck: { score: 9.5 }
AssignmentInConditionalCheck: { }
CaseMissingElseCheck: { }
-ClassLineCountCheck: { line_count: 360 }
+ClassLineCountCheck: { line_count: 400 }
ClassNameCheck: { pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/ }
ClassVariableCheck: { }
CyclomaticComplexityBlockCheck: { complexity: 2 }
@@ -12,7 +12,7 @@ ForLoopCheck: { }
# TODO: decrease line_count to 5 to 10
MethodLineCountCheck: { line_count: 17 }
MethodNameCheck: { pattern: !ruby/regexp /\A(?:[a-z\d](?:_?[a-z\d])+[?!=]?|\[\]=?|==|<=>|<<|[+*&|-])\z/ }
-ModuleLineCountCheck: { line_count: 366 }
+ModuleLineCountCheck: { line_count: 406 }
ModuleNameCheck: { pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/ }
# TODO: decrease parameter_count to 2 or less
ParameterNumberCheck: { parameter_count: 3 }
View
@@ -22,74 +22,6 @@ def self.included(base)
base.send(:include, InstanceMethods)
end
- # Returns a Virtus::Attribute::Object descendant based on a name or class
- #
- # @example
- # Virtus.determine_type('String') # => Virtus::Attribute::String
- #
- # @param [Class, #to_s] class_or_name
- # name of a class or a class itself
- #
- # @return [Class]
- # one of the Virtus::Attribute::Object descendants
- #
- # @return [nil]
- # nil if the type cannot be determined by the class_or_name
- #
- # @api semipublic
- def self.determine_type(class_or_name)
- if class_or_name.kind_of?(Class)
- if class_or_name < Attribute::Object
- determine_type_from_attribute(class_or_name)
- else
- determine_type_from_primitive(class_or_name)
- end
- else
- determine_type_from_string(class_or_name.to_s)
- end
- end
-
- # Return the Attribute class given an Attribute descendant
- #
- # @param [Class<Attribute>] attribute
- #
- # @return [Class<Attribute>]
- #
- # @api private
- def self.determine_type_from_attribute(attribute)
- attribute
- end
-
- # Return the Attribute class given a primitive
- #
- # @param [Class] primitive
- #
- # @return [Class<Attribute>]
- #
- # @return [nil]
- # nil if the type cannot be determined by the primitive
- #
- # @api private
- def self.determine_type_from_primitive(primitive)
- Attribute.from_primitive(primitive)
- end
-
- # Return the Attribute class given a string
- #
- # @param [String] string
- #
- # @return [Class<Attribute>]
- #
- # @return [nil]
- # nil if the type cannot be determined by the string
- #
- # @api private
- def self.determine_type_from_string(string)
- Attribute.from_string(string)
- end
-
- private_class_method :determine_type_from_attribute, :determine_type_from_primitive, :determine_type_from_string
-
end # module Virtus
require 'virtus/support/descendants_tracker'
View
@@ -6,6 +6,44 @@ module Virtus
class Attribute
extend DescendantsTracker
+ # Returns a Virtus::Attribute::Object descendant based on a name or class
+ #
+ # @example
+ # Attribute.determine_type('String') # => Virtus::Attribute::String
+ #
+ # @param [Class, #to_s] class_or_name
+ # name of a class or a class itself
+ #
+ # @return [Class]
+ # one of the Virtus::Attribute::Object descendants
+ #
+ # @return [nil]
+ # nil if the type cannot be determined by the class_or_name
+ #
+ # @api semipublic
+ def self.determine_type(class_or_name)
+ if class_or_name.kind_of?(Class)
+ if class_or_name < Attribute::Object
+ determine_type_from_attribute(class_or_name)
+ else
+ determine_type_from_primitive(class_or_name)
+ end
+ else
+ determine_type_from_string(class_or_name.to_s)
+ end
+ end
+
+ # Return the Attribute class given an Attribute descendant
+ #
+ # @param [Class<Attribute>] attribute
+ #
+ # @return [Class<Attribute>]
+ #
+ # @api private
+ def self.determine_type_from_attribute(attribute)
+ attribute
+ end
+
# Return the Attribute class given a primitive
#
# @param [Class] primitive
@@ -16,7 +54,7 @@ class Attribute
# nil if the type cannot be determined by the primitive
#
# @api private
- def self.from_primitive(primitive)
+ def self.determine_type_from_primitive(primitive)
descendants.detect { |descendant| primitive <= descendant.primitive }
end
@@ -30,10 +68,12 @@ def self.from_primitive(primitive)
# nil if the type cannot be determined by the string
#
# @api private
- def self.from_string(string)
+ def self.determine_type_from_string(string)
const_get(string) if const_defined?(string)
end
+ private_class_method :determine_type_from_attribute, :determine_type_from_primitive, :determine_type_from_string
+
# Returns default options hash for a given attribute class
#
# @example
@@ -28,7 +28,7 @@ module ClassMethods
#
# @api public
def attribute(name, type, options = {})
- attribute = Virtus.determine_type(type).new(name, options)
+ attribute = Attribute.determine_type(type).new(name, options)
attribute.add_reader_method(self)
attribute.add_writer_method(self)
@@ -79,7 +79,7 @@ def attributes
#
# @api private
def const_missing(name)
- Virtus.determine_type(name) || super
+ Attribute.determine_type(name) || super
end
end # module ClassMethods
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+describe Virtus::Attribute, '.determine_type' do
+ let(:object) { described_class }
+
+ described_class.descendants.each do |attribute_class|
+ context "with class #{attribute_class.inspect}" do
+ subject { object.determine_type(attribute_class) }
+
+ it 'returns the corresponding attribute class' do
+ should be(attribute_class)
+ end
+ end
+
+ primitive = attribute_class.primitive
+ context "with primitive #{primitive.inspect}" do
+ subject { object.determine_type(primitive) }
+
+ it 'returns the corresponding attribute class' do
+ should be(attribute_class)
+ end
+ end
+
+ string = attribute_class.name['Virtus::Attribute::'.length..-1]
+ context "with string #{string.inspect}" do
+ subject { object.determine_type(string) }
+
+ it 'returns the corresponding attribute class' do
+ should be(attribute_class)
+ end
+ end
+ end
+
+ context 'when the primitive defaults to Object' do
+ subject { object.determine_type(primitive) }
+
+ let(:primitive) { Class.new }
+
+ it { should equal(Virtus::Attribute::Object) }
+ end
+
+ context 'when the string does not map to an Attribute' do
+ subject { object.determine_type(string) }
+
+ let(:string) { 'Unknown' }
+
+ it { should be_nil }
+ end
+end
@@ -1,19 +0,0 @@
-require 'spec_helper'
-
-describe Virtus::Attribute, '.from_primitive' do
- subject { object.from_primitive(primitive) }
-
- let(:object) { described_class }
-
- context 'when the primitive maps to an Attribute' do
- let(:primitive) { String }
-
- it { should equal(Virtus::Attribute::String) }
- end
-
- context 'when the primitive defaults to Object' do
- let(:primitive) { Class.new }
-
- it { should equal(Virtus::Attribute::Object) }
- end
-end
@@ -1,19 +0,0 @@
-require 'spec_helper'
-
-describe Virtus::Attribute, '.from_string' do
- subject { object.from_string(string.to_s) }
-
- let(:object) { described_class }
-
- context 'when the string maps to an Attribute' do
- let(:string) { 'String' }
-
- it { should equal(Virtus::Attribute::String) }
- end
-
- context 'when the string does not map to an Attribute' do
- let(:string) { 'Unknown' }
-
- it { should be_nil }
- end
-end
@@ -1,32 +0,0 @@
-require 'spec_helper'
-
-describe Virtus, '.determine_type' do
- Virtus::Attribute.descendants.each do |attribute_class|
-
- context "with class #{attribute_class.inspect}" do
- subject { described_class.determine_type(attribute_class) }
-
- it 'returns the corresponding attribute class' do
- should be(attribute_class)
- end
- end
-
- primitive = attribute_class.primitive
- context "with primitive #{primitive.inspect}" do
- subject { described_class.determine_type(primitive) }
-
- it 'returns the corresponding attribute class' do
- should be(attribute_class)
- end
- end
-
- suffix = attribute_class.name['Virtus::Attribute::'.length..-1]
- context "with string #{suffix.inspect}" do
- subject { described_class.determine_type(suffix) }
-
- it 'returns the corresponding attribute class' do
- should be(attribute_class)
- end
- end
- end
-end
View
@@ -72,8 +72,7 @@ Gem::Specification.new do |s|
"spec/unit/virtus/attribute/array_spec.rb",
"spec/unit/virtus/attribute/attribute_spec.rb",
"spec/unit/virtus/attribute/boolean_spec.rb",
- "spec/unit/virtus/attribute/class_methods/from_primitive_spec.rb",
- "spec/unit/virtus/attribute/class_methods/from_string_spec.rb",
+ "spec/unit/virtus/attribute/class_methods/determine_type_spec.rb",
"spec/unit/virtus/attribute/date_spec.rb",
"spec/unit/virtus/attribute/date_time_spec.rb",
"spec/unit/virtus/attribute/decimal_spec.rb",
@@ -95,7 +94,6 @@ Gem::Specification.new do |s|
"spec/unit/virtus/class_methods/new_spec.rb",
"spec/unit/virtus/descendants_tracker/descendants_spec.rb",
"spec/unit/virtus/descendants_tracker/inherited_spec.rb",
- "spec/unit/virtus/determine_type_spec.rb",
"spec/unit/virtus/instance_methods/attributes_spec.rb",
"spec/unit/virtus/instance_methods/element_reference_spec.rb",
"spec/unit/virtus/instance_methods/element_set_spec.rb",
@@ -115,7 +113,7 @@ Gem::Specification.new do |s|
s.require_paths = [%q{lib}]
s.rubygems_version = %q{1.8.5}
s.summary = %q{Attributes for your plain ruby objects}
- s.test_files = [%q{spec/integration/virtus/attributes/attribute/typecast_spec.rb}, %q{spec/integration/virtus/class_methods/attribute_spec.rb}, %q{spec/integration/virtus/class_methods/attributes_spec.rb}, %q{spec/integration/virtus/class_methods/const_missing_spec.rb}, %q{spec/rcov.opts}, %q{spec/shared/idempotent_method_behaviour.rb}, %q{spec/spec_helper.rb}, %q{spec/unit/shared/attribute.rb}, %q{spec/unit/shared/attribute/accept_options.rb}, %q{spec/unit/shared/attribute/accepted_options.rb}, %q{spec/unit/shared/attribute/complex.rb}, %q{spec/unit/shared/attribute/get.rb}, %q{spec/unit/shared/attribute/options.rb}, %q{spec/unit/shared/attribute/set.rb}, %q{spec/unit/virtus/attribute/array_spec.rb}, %q{spec/unit/virtus/attribute/attribute_spec.rb}, %q{spec/unit/virtus/attribute/boolean_spec.rb}, %q{spec/unit/virtus/attribute/class_methods/from_primitive_spec.rb}, %q{spec/unit/virtus/attribute/class_methods/from_string_spec.rb}, %q{spec/unit/virtus/attribute/date_spec.rb}, %q{spec/unit/virtus/attribute/date_time_spec.rb}, %q{spec/unit/virtus/attribute/decimal_spec.rb}, %q{spec/unit/virtus/attribute/float_spec.rb}, %q{spec/unit/virtus/attribute/hash_spec.rb}, %q{spec/unit/virtus/attribute/integer_spec.rb}, %q{spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb}, %q{spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb}, %q{spec/unit/virtus/attribute/string_spec.rb}, %q{spec/unit/virtus/attribute/time_spec.rb}, %q{spec/unit/virtus/attribute_set/append_spec.rb}, %q{spec/unit/virtus/attribute_set/each_spec.rb}, %q{spec/unit/virtus/attribute_set/element_reference_spec.rb}, %q{spec/unit/virtus/attribute_set/element_set_spec.rb}, %q{spec/unit/virtus/attribute_set/merge_spec.rb}, %q{spec/unit/virtus/attribute_set/parent_spec.rb}, %q{spec/unit/virtus/attribute_set/reset_spec.rb}, %q{spec/unit/virtus/class_methods/attribute_spec.rb}, %q{spec/unit/virtus/class_methods/new_spec.rb}, %q{spec/unit/virtus/descendants_tracker/descendants_spec.rb}, %q{spec/unit/virtus/descendants_tracker/inherited_spec.rb}, %q{spec/unit/virtus/determine_type_spec.rb}, %q{spec/unit/virtus/instance_methods/attributes_spec.rb}, %q{spec/unit/virtus/instance_methods/element_reference_spec.rb}, %q{spec/unit/virtus/instance_methods/element_set_spec.rb}]
+ s.test_files = [%q{spec/integration/virtus/attributes/attribute/typecast_spec.rb}, %q{spec/integration/virtus/class_methods/attribute_spec.rb}, %q{spec/integration/virtus/class_methods/attributes_spec.rb}, %q{spec/integration/virtus/class_methods/const_missing_spec.rb}, %q{spec/rcov.opts}, %q{spec/shared/idempotent_method_behaviour.rb}, %q{spec/spec_helper.rb}, %q{spec/unit/shared/attribute.rb}, %q{spec/unit/shared/attribute/accept_options.rb}, %q{spec/unit/shared/attribute/accepted_options.rb}, %q{spec/unit/shared/attribute/complex.rb}, %q{spec/unit/shared/attribute/get.rb}, %q{spec/unit/shared/attribute/options.rb}, %q{spec/unit/shared/attribute/set.rb}, %q{spec/unit/virtus/attribute/array_spec.rb}, %q{spec/unit/virtus/attribute/attribute_spec.rb}, %q{spec/unit/virtus/attribute/boolean_spec.rb}, %q{spec/unit/virtus/attribute/class_methods/determine_type_spec.rb}, %q{spec/unit/virtus/attribute/date_spec.rb}, %q{spec/unit/virtus/attribute/date_time_spec.rb}, %q{spec/unit/virtus/attribute/decimal_spec.rb}, %q{spec/unit/virtus/attribute/float_spec.rb}, %q{spec/unit/virtus/attribute/hash_spec.rb}, %q{spec/unit/virtus/attribute/integer_spec.rb}, %q{spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb}, %q{spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb}, %q{spec/unit/virtus/attribute/string_spec.rb}, %q{spec/unit/virtus/attribute/time_spec.rb}, %q{spec/unit/virtus/attribute_set/append_spec.rb}, %q{spec/unit/virtus/attribute_set/each_spec.rb}, %q{spec/unit/virtus/attribute_set/element_reference_spec.rb}, %q{spec/unit/virtus/attribute_set/element_set_spec.rb}, %q{spec/unit/virtus/attribute_set/merge_spec.rb}, %q{spec/unit/virtus/attribute_set/parent_spec.rb}, %q{spec/unit/virtus/attribute_set/reset_spec.rb}, %q{spec/unit/virtus/class_methods/attribute_spec.rb}, %q{spec/unit/virtus/class_methods/new_spec.rb}, %q{spec/unit/virtus/descendants_tracker/descendants_spec.rb}, %q{spec/unit/virtus/descendants_tracker/inherited_spec.rb}, %q{spec/unit/virtus/instance_methods/attributes_spec.rb}, %q{spec/unit/virtus/instance_methods/element_reference_spec.rb}, %q{spec/unit/virtus/instance_methods/element_set_spec.rb}]
if s.respond_to? :specification_version then
s.specification_version = 3

0 comments on commit 3b0df3b

Please sign in to comment.