Permalink
Browse files

Factored out primitive/string type lookup to Attribute

  • Loading branch information...
1 parent a85b1fb commit 880a097a605c27166fcd7de07d4ed32e4406e4a9 @dkubb dkubb committed Jul 8, 2011
View
@@ -1,3 +1,3 @@
---
threshold: 19.0
-total_score: 200
+total_score: 201
View
@@ -2,7 +2,7 @@
AbcMetricMethodCheck: { score: 9.5 }
AssignmentInConditionalCheck: { }
CaseMissingElseCheck: { }
-ClassLineCountCheck: { line_count: 345 }
+ClassLineCountCheck: { line_count: 360 }
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: 351 }
+ModuleLineCountCheck: { line_count: 366 }
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
@@ -71,9 +71,7 @@ def self.determine_type_from_attribute(attribute)
#
# @api private
def self.determine_type_from_primitive(primitive)
- Attribute.descendants.detect do |descendant|
- primitive <= descendant.primitive
- end
+ Attribute.from_primitive(primitive)
end
# Return the Attribute class given a string
@@ -87,7 +85,7 @@ def self.determine_type_from_primitive(primitive)
#
# @api private
def self.determine_type_from_string(string)
- Attribute.const_get(string) if Attribute.const_defined?(string)
+ Attribute.from_string(string)
end
private_class_method :determine_type_from_attribute, :determine_type_from_primitive, :determine_type_from_string
View
@@ -6,6 +6,34 @@ module Virtus
class Attribute
extend DescendantsTracker
+ # 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.from_primitive(primitive)
+ descendants.detect { |descendant| primitive <= descendant.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.from_string(string)
+ const_get(string) if const_defined?(string)
+ end
+
# Returns default options hash for a given attribute class
#
# @example
@@ -0,0 +1,19 @@
+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
@@ -0,0 +1,19 @@
+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
View
@@ -8,10 +8,10 @@ Gem::Specification.new do |s|
s.version = "0.0.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Piotr Solnica"]
+ s.authors = [%q{Piotr Solnica}]
s.date = %q{2011-07-08}
s.description = %q{Attributes for your plain ruby objects}
- s.email = ["piotr@rubyverse.com"]
+ s.email = [%q{piotr@rubyverse.com}]
s.extra_rdoc_files = [
"LICENSE",
"README.markdown",
@@ -72,6 +72,8 @@ 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/date_spec.rb",
"spec/unit/virtus/attribute/date_time_spec.rb",
"spec/unit/virtus/attribute/decimal_spec.rb",
@@ -110,10 +112,10 @@ Gem::Specification.new do |s|
"virtus.gemspec"
]
s.homepage = %q{https://github.com/solnic/virtus}
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.6.2}
+ s.require_paths = [%q{lib}]
+ s.rubygems_version = %q{1.8.5}
s.summary = %q{Attributes for your plain ruby objects}
- s.test_files = ["spec/integration/virtus/attributes/attribute/typecast_spec.rb", "spec/integration/virtus/class_methods/attribute_spec.rb", "spec/integration/virtus/class_methods/attributes_spec.rb", "spec/integration/virtus/class_methods/const_missing_spec.rb", "spec/rcov.opts", "spec/shared/idempotent_method_behaviour.rb", "spec/spec_helper.rb", "spec/unit/shared/attribute.rb", "spec/unit/shared/attribute/accept_options.rb", "spec/unit/shared/attribute/accepted_options.rb", "spec/unit/shared/attribute/complex.rb", "spec/unit/shared/attribute/get.rb", "spec/unit/shared/attribute/options.rb", "spec/unit/shared/attribute/set.rb", "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/date_spec.rb", "spec/unit/virtus/attribute/date_time_spec.rb", "spec/unit/virtus/attribute/decimal_spec.rb", "spec/unit/virtus/attribute/float_spec.rb", "spec/unit/virtus/attribute/hash_spec.rb", "spec/unit/virtus/attribute/integer_spec.rb", "spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb", "spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb", "spec/unit/virtus/attribute/string_spec.rb", "spec/unit/virtus/attribute/time_spec.rb", "spec/unit/virtus/attribute_set/append_spec.rb", "spec/unit/virtus/attribute_set/each_spec.rb", "spec/unit/virtus/attribute_set/element_reference_spec.rb", "spec/unit/virtus/attribute_set/element_set_spec.rb", "spec/unit/virtus/attribute_set/merge_spec.rb", "spec/unit/virtus/attribute_set/parent_spec.rb", "spec/unit/virtus/attribute_set/reset_spec.rb", "spec/unit/virtus/class_methods/attribute_spec.rb", "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"]
+ 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}]
if s.respond_to? :specification_version then
s.specification_version = 3

0 comments on commit 880a097

Please sign in to comment.