Module: Eco::API::Common::ClassHelpers

Included in:
ClassAutoLoader, ClassHierarchy, Loaders::Base, Loaders::Config, UseCases::BaseCase, UseCases::Cli
Defined in:
lib/eco/api/common/class_helpers.rb

Instance Method Summary collapse

Instance Method Details

#class_resolver(name, klass) ⇒ Object

Creates a class and instance object methods with name name to resolve klass name



6
7
8
9
# File 'lib/eco/api/common/class_helpers.rb', line 6

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.

Parameters:

  • parent_class (Class) (defaults to: self)

    the parent class we want to find children of.

  • direct (Boolean) (defaults to: false)

    it will only include direct child classes.

  • scope (nil, Array) (defaults to: nil)

    to only look for descendants among the ones in scope.

Returns:

  • (Arrary<Class>)

    the child classes in hierarchy order.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/eco/api/common/class_helpers.rb', line 78

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 |k_1, k_2|
    next -1 if k_2 < k_1
    next  1 if k_1 < k_2
    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.

Parameters:

  • parent_class (Class) (defaults to: self)

    the parent class we want to find children of.

  • direct (Boolean) (defaults to: false)

    it will only include direct child classes.

Returns:

  • (Boolean)

    true if the current class has child classes, and false otherwise.



99
100
101
# File 'lib/eco/api/common/class_helpers.rb', line 99

def descendants?(parent_class: self, direct: false)
  descendants(parent_class: parent_class, direct: direct).length.positive?
end

#inheritable_attrs(*attrs) ⇒ Object

Builds the attr_reader and attr_writer of attrs and registers the associated instance variable as inheritable.



117
118
119
120
121
122
123
124
# File 'lib/eco/api/common/class_helpers.rb', line 117

def inheritable_attrs(*attrs)
  attrs.each do |attr|
    class_eval %(
      class << self; attr_accessor :#{attr} end
    ), __FILE__, __LINE__ - 2
  end
  inheritable_class_vars(*attrs)
end

#inheritable_class_vars(*vars) ⇒ Object

Note:

Keeps track on class instance variables that should be inherited by child classes. TODO: this separates the logic of the method to the instance var. Think if would be possible to join them somehow.



110
111
112
113
# File 'lib/eco/api/common/class_helpers.rb', line 110

def inheritable_class_vars(*vars)
  @inheritable_class_vars ||= [:inheritable_class_vars]
  @inheritable_class_vars += vars
end

#inherited(subclass) ⇒ Object

Note:
  • values of the instance variables are copied as they are (no dups or clones)
  • the above means: avoid methods that change the state of the mutable object on it
  • mutating methods would reflect the changes on other classes as well
  • therefore, freeze will be called on the values that are inherited.

This callback method is called whenever a subclass of the current class is created.



132
133
134
135
136
137
138
139
# File 'lib/eco/api/common/class_helpers.rb', line 132

def inherited(subclass)
  super
  inheritable_class_vars.each do |var|
    instance_var = instance_variable_name(var)
    value        = instance_variable_get(instance_var)
    subclass.instance_variable_set(instance_var, value.freeze)
  end
end

#instance_variable_name(name) ⇒ Object

Helper to create an instance variable name

Parameters:

  • the (String, Symbol)

    name of the variable



44
45
46
47
48
# File 'lib/eco/api/common/class_helpers.rb', line 44

def instance_variable_name(name)
  str = name.to_s
  str = "@#{str}" unless str.start_with?("@")
  str
end

#new_class(name, inherits:, parent_space: nil) {|child_class| ... } ⇒ Class

If the class for name exists, it returns it. Otherwise it generates it.

Parameters:

  • name (String, Symbol)

    the name of the new class

  • inherits (Class)

    the parent class to inherit from

  • parent_space (String) (defaults to: nil)

    parent namespace of the generated class, if not given: self

Yields:

  • (child_class)

    configure the new class

Yield Parameters:

  • child_class (Class)

    the new class

Returns:

  • (Class)

    the new generated class



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eco/api/common/class_helpers.rb', line 57

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

Returns:

  • (Class)

    the class that was being searched by name klass.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eco/api/common/class_helpers.rb', line 13

def resolve_class(klass, exception: true)
  @resolved ||= {}
  @resolved[klass] ||=
    case klass
    when Class
      klass
    when String
      begin
        Kernel.const_get(klass)
      rescue NameError
        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

Parameters:

  • key (String, Symbol)

    to be normalized

Returns:

  • (String)

    a correct constant name



35
36
37
38
39
# File 'lib/eco/api/common/class_helpers.rb', line 35

def to_constant(key)
  key.to_s.strip.split(/[\-\_ ]/i).compact.map do |str|
    str.slice(0).upcase + str.slice(1..-1).downcase
  end.join
end