Module: Eco::API::Common::ClassHelpers
- Included in:
- BaseLoader, ClassAutoLoader, ClassHierarchy, UseCases::BaseCase
- Defined in:
- lib/eco/api/common/class_helpers.rb
Instance Method Summary collapse
-
#class_resolver(name, klass) ⇒ Object
Creates an class and instance object methods with name
name
to resolveklass
name. -
#descendants(parent_class: self, direct: false, scope: nil) ⇒ Arrary<Class>
Finds all child classes of the current class.
-
#descendants?(parent_class: self, direct: false) ⇒ Boolean
true
if the current class has child classes, andfalse
otherwise. -
#new_class(name, inherits:, parent_space: nil) {|child_class| ... } ⇒ Class
If the class for
name
exists, it returns it. -
#resolve_class(klass, exception: true) ⇒ Class
With given a
klass
name it resolves to an actualClass
. -
#to_constant(key) ⇒ String
Helper to normalize
key
into a correctruby
constant name.
Instance Method Details
#class_resolver(name, klass) ⇒ Object
Creates an class and instance object methods with name name
to resolve klass
name
7 8 9 10 |
# File 'lib/eco/api/common/class_helpers.rb', line 7 def class_resolver(name, klass) define_singleton_method(name) { resolve_class(klass) } define_method(name) { self.class.resolve_class(klass) } end |
#descendants(parent_class: self, direct: false, scope: nil) ⇒ Arrary<Class>
Finds all child classes of the current class.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/eco/api/common/class_helpers.rb', line 70 def descendants(parent_class: self, direct: false, scope: nil) scope ||= ObjectSpace.each_object(::Class) return [] if scope.empty? scope.select do |klass| klass < parent_class end.sort do |k1, k2| next -1 if k2 < k1 next 1 if k1 < k2 0 end.tap do |siblings| if direct siblings.reject! do |si| siblings.any? {|s| si < s} end end end end |
#descendants?(parent_class: self, direct: false) ⇒ Boolean
Returns true
if the current class has child classes, and false
otherwise.
91 92 93 |
# File 'lib/eco/api/common/class_helpers.rb', line 91 def descendants?(parent_class: self, direct: false) descendants(parent_class: parent_class, direct: direct).length > 0 end |
#new_class(name, inherits:, parent_space: nil) {|child_class| ... } ⇒ Class
If the class for name
exists, it returns it. Otherwise it generates it.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/eco/api/common/class_helpers.rb', line 49 def new_class(name, inherits:, parent_space: nil) name = name.to_sym.freeze class_name = to_constant(name) parent_space = parent_space ? resolve_class(parent_space) : self full_class_name = "#{parent_space}::#{class_name}" unless target_class = resolve_class(full_class_name, exception: false) target_class = Class.new(inherits) parent_space.const_set class_name, target_class end target_class.tap do |klass| yield(klass) if block_given? end end |
#resolve_class(klass, exception: true) ⇒ Class
With given a klass
name it resolves to an actual Class
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eco/api/common/class_helpers.rb', line 14 def resolve_class(klass, exception: true) @resolved ||= {} @resolved[klass] ||= case klass when Class klass when String begin Kernel.const_get(klass) rescue NameError => e raise if exception end when Symbol resolve_class(self.send(klass)) else raise "Unknown class: #{klass}" if exception end end |
#to_constant(key) ⇒ String
Helper to normalize key
into a correct ruby
constant name
36 37 38 39 40 |
# File 'lib/eco/api/common/class_helpers.rb', line 36 def to_constant(key) str_name = key.to_s.strip.split(/[\-\_ ]/i).compact.map do |str| str.slice(0).upcase + str.slice(1..-1).downcase end.join("") end |