lib/emittance/helpers/constant_helpers.rb in emittance-0.0.5 vs lib/emittance/helpers/constant_helpers.rb in emittance-0.0.6
- old
+ new
@@ -4,11 +4,11 @@
module Helpers
##
# Some helpers for fetching, setting, and manipulating constants.
#
module ConstantHelpers
- # Since +Object.const_set+ does not support namespaced constant names, use this to set the constant to the
+ # Since +Object#const_set+ does not support namespaced constant names, use this to set the constant to the
# correct namespace.
#
# Example:
#
# my_const_name = 'String::Foo'
@@ -19,15 +19,32 @@
# String::Foo
# # => 'bar'
#
# @param const_name [String] a valid namespaced constant name
# @param obj the value you wish to set that constant to
- def set_namespaced_constant_by_name(const_name, obj)
+ # @return the object to which you assigned the namespaced constant
+ def set_namespaced_constant_by_name(const_name, obj, namespace_filler_klass = Module)
names = const_name.split('::')
names.shift if names.size > 1 && names.first.empty?
- namespace = names.size == 1 ? Object : Object.const_get(names[0...-1].join('::'))
+ parent_names = names[0...-1]
+ parent_namespace_name = names[0...-1].join('::')
+
+ fill_in_namespace(parent_names, parent_namespace_name, namespace_filler_klass)
+
+ namespace = parent_names.empty? ? Object : Object.const_get(parent_namespace_name)
namespace.const_set names.last, obj
end
+
+ private
+
+ def fill_in_namespace(parent_names, parent_namespace_name, namespace_filler_klass)
+ set_namespaced_constant_by_name parent_namespace_name, namespace_filler_klass.new, namespace_filler_klass \
+ unless parent_names.empty? || Object.const_defined?(parent_namespace_name)
+ end
end
end
end
+
+# class Foo
+# extend Emittance::Helpers::ConstantHelpers
+# end