Permalink
Browse files

Split up Virtus.determine_type

  • Loading branch information...
1 parent 8926d10 commit 07badb1ab41d302fa335cbbe50a4d7c2b4af293a @dkubb dkubb committed Jul 8, 2011
Showing with 47 additions and 6 deletions.
  1. +47 −6 lib/virtus.rb
View
53 lib/virtus.rb
@@ -37,17 +37,58 @@ def self.included(base)
def self.determine_type(class_or_name)
if class_or_name.kind_of?(Class)
if class_or_name < Attribute::Object
- class_or_name
+ determine_type_from_attribute(class_or_name)
else
- Attribute.descendants.detect do |descendant|
- class_or_name <= descendant.primitive
- end
+ determine_type_from_primitive(class_or_name)
end
- elsif Attribute.const_defined?(name = class_or_name.to_s)
- Attribute.const_get(name)
+ 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 primitive does not map to an Attribute
+ #
+ # @api private
+ def self.determine_type_from_primitive(primitive)
+ Attribute.descendants.detect do |descendant|
+ primitive <= descendant.primitive
+ end
+ end
+
+ # Return the Attribute class given a string
+ #
+ # @param [String]
+ #
+ # @return [Class<Attribute>]
+ #
+ # @return [nil]
+ # nil if the string is not a constant in the Attribute namespace
+ #
+ # @api private
+ def self.determine_type_from_string(string)
+ Attribute.const_get(string) if Attribute.const_defined?(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'

0 comments on commit 07badb1

Please sign in to comment.