Module: CsvRowModel::Concerns::InheritedClassVar

Extended by:
ActiveSupport::Concern
Included in:
Import::Presenter, Model
Defined in:
lib/csv_row_model/concerns/inherited_class_var.rb

Class Method Summary collapse

Class Method Details

.class_cache(variable_name) ⇒ Object (protected)

Memozies a inherited_class_variable_name

Parameters:

  • variable_name (Symbol)

    variable_name to cache against



76
77
78
79
80
81
82
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 76

def class_cache(variable_name)
  #
  # equal to: (has @)inherited_class_variable_name ||= yield
  #
  cache_variable_name = inherited_class_variable_name(variable_name)
  instance_variable_get(cache_variable_name) || instance_variable_set(cache_variable_name, yield)
end

.clear_class_cache(variable_name) ⇒ Object

Clears the cache for a variable

Parameters:

  • variable_name (Symbol)

    variable_name to cache against



9
10
11
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 9

def clear_class_cache(variable_name)
  instance_variable_set inherited_class_variable_name(variable_name), nil
end

.deep_clear_class_cache(variable_name) ⇒ Object (protected)

Clears the cache for a variable and the same variable for all it's dependant descendants

Parameters:

  • variable_name (Symbol)

    variable_name to cache against



68
69
70
71
72
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 68

def deep_clear_class_cache(variable_name)
  ([self] + descendants).each do |descendant|
    descendant.try(:clear_class_cache, variable_name)
  end
end

.inherited_ancestors(included_module = inherited_class_module) ⇒ Array<Module> (protected)

Returns inherited_ancestors of included_module (including self).

Parameters:

  • included_module (Module) (defaults to: inherited_class_module)

    module to search for

Returns:

  • (Array<Module>)

    inherited_ancestors of included_module (including self)



17
18
19
20
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 17

def inherited_ancestors(included_module=inherited_class_module)
  included_model_index = ancestors.index(included_module)
  included_model_index == 0 ? [included_module] : ancestors[0..(included_model_index - 1)]
end

.inherited_class_var(variable_name, default_value, merge_method) ⇒ Object (protected)

Returns a class variable merged across ancestors until inherited_class_module.

Parameters:

  • variable_name (Symbol)

    class variable name (recommend :@_variable_name)

  • default_value (Object)

    default value of the class variable

  • merge_method (Symbol)

    method to merge values of the class variable

Returns:

  • (Object)

    a class variable merged across ancestors until inherited_class_module



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 47

def inherited_class_var(variable_name, default_value, merge_method)
  class_cache(variable_name) do
    value = default_value

    inherited_ancestors.each do |ancestor|
      ancestor_value = ancestor.instance_variable_get(variable_name)
      value = ancestor_value.public_send(merge_method, value) if ancestor_value.present?
    end

    value
  end
end

.inherited_class_variable_name(variable_name) ⇒ String (protected)

Returns the cache variable name for the cache.

Parameters:

  • variable_name (Symbol)

    variable_name to cache against

Returns:

  • (String)

    the cache variable name for the cache



62
63
64
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 62

def inherited_class_variable_name(variable_name)
  "#{variable_name}_inherited_class_cache"
end

.inherited_custom_class(accessor_method_name, base_parent_class) ⇒ Class (protected)

grandparent -> parent -> self

grandparent has inherited_custom_class, but parent, doesn't.

then: base_parent_class -> grandparent::inherited_custom_class -> self::inherited_custom_class

Parameters:

  • accessor_method_name (Symbol)

    method to access the inherited_custom_class

  • base_parent_class (Class)

    class that the custom class inherits from if there's no parent

Returns:

  • (Class)

    a custom class with the inheritance following self. for example:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/csv_row_model/concerns/inherited_class_var.rb', line 31

def inherited_custom_class(accessor_method_name, base_parent_class)
  parent_class = inherited_ancestors[1..-1].find do |klass|
    klass.respond_to?(accessor_method_name)
  end.try(accessor_method_name)
  parent_class ||= base_parent_class

  klass = Class.new(parent_class)
  # how else can i get the current scopes name...
  klass.send(:define_singleton_method, :name, &eval("-> { \"#{name}#{base_parent_class.name.demodulize}\" }"))
  klass
end