Module: RakeCommander::Base::ClassInheritable

Includes:
ObjectHelpers
Defined in:
lib/rake-commander/base/class_inheritable.rb

Instance Method Summary collapse

Instance Method Details

#attr_inheritable(*attrs, add_accessors: false, deep_dup: true) {|value| ... } ⇒ Object

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

Parameters:

  • attrs (Array <Symbol>)

    the variable names that should be inheritable.

  • add_accessors (Boolean) (defaults to: false)

    whether attr_accessor should be invoked

  • deep_dup (Boolean) (defaults to: true)

    whether the value of the instance var should be deep_duped.

Yields:

  • (value)

Yield Parameters:

  • value (Variant)

    the value of the parent class

Yield Returns:

  • the value that will be inherited by the child class



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rake-commander/base/class_inheritable.rb', line 14

def attr_inheritable(*attrs, add_accessors: false, deep_dup: true, &block)
  attrs = attrs.map(&:to_sym)
  inheritable_class_var(*attrs, deep_dup: deep_dup, &block)
  return unless add_accessors
  attrs.each do |attr|
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      class << self; attr_accessor :#{attr} end
    RUBY
  end
  self
end

#attr_not_inheritable(*attrs) ⇒ Object

Removes from the inheritance some class variables.

Parameters:

  • attrs (Array <Symbol>)

    the instance variable names of the class that should NOT be inheritable.



36
37
38
39
40
41
42
# File 'lib/rake-commander/base/class_inheritable.rb', line 36

def attr_not_inheritable(*attrs)
  attrs.each do |attr|
    next unless method = inheritable_class_var_method(attr)
    inheritable_class_var[method].delete(attr)
  end
  self
end

#inheritable_class_var?(var) ⇒ Boolean

Returns whether an var has been declared as inheritable.

Returns:

  • (Boolean)

    whether an var has been declared as inheritable



27
28
29
30
31
# File 'lib/rake-commander/base/class_inheritable.rb', line 27

def inheritable_class_var?(var)
  inheritable_class_var.any? do |_method, definitions|
    definitions.key?(var.to_sym)
  end
end