class JkoApi::ClassDescendantsBuilder
Constants
- LEVEL_REGEX
Attributes
descendant[R]
upto[R]
Public Class Methods
build(base_class, upto:)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 5 def self.build(base_class, upto:) base_class.descendants.each do |descendant| new(descendant, upto).build end end
new(descendant, upto)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 13 def initialize(descendant, upto) @descendant, @upto = descendant, upto end
Public Instance Methods
build()
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 17 def build initial_level.upto(upto) do |level| unless Module.const_defined?(swap_level(level)) build_descendant level.pred end end end
Private Instance Methods
build_descendant(level)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 27 def build_descendant(level) namespace(level).const_set( swap_level(level).demodulize, Class.new(swap_level(level.next).constantize) ) end
initial_level()
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 46 def initial_level descendant.name[LEVEL_REGEX].to_i end
namespace(level)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 34 def namespace(level) deconstantized = swap_level(level).deconstantize unless qualified_const_defined?(deconstantized) qualified_const_set(deconstantized, Module.new) end deconstantized.constantize end
qualified_const_defined?(path)
click to toggle source
Pulled from Rails 4.2 source
# File lib/jko_api/class_descendants_builder.rb, line 51 def qualified_const_defined?(path) raise NameError, "#{path.inspect} is not a valid constant name!" unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path names = path.to_s.split('::') names.shift if names.first.empty? # We can't use defined? because it will invoke const_missing for the parent # of the name we are checking. names.inject(Module) do |mod, name| return false unless mod.const_defined?(name, false) mod.const_get name end return true end
qualified_const_get(path)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 74 def qualified_const_get(path) path.split('::').inject(Module) do |mod, name| mod.const_get(name) end end
qualified_const_set(path, value)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 66 def qualified_const_set(path, value) raise NameError.new("wrong constant name #$&") if path =~ /\A::[^:]+/ const_name = path.demodulize mod_name = path.deconstantize mod = mod_name.empty? ? self : qualified_const_get(mod_name) mod.const_set(const_name, value) unless mod.const_defined?(const_name) end
swap_level(level)
click to toggle source
# File lib/jko_api/class_descendants_builder.rb, line 42 def swap_level(level) descendant.name.sub LEVEL_REGEX, level.to_s end