Module: Foobara::Concern::IsConcern

Defined in:
foobara-0.0.110/projects/concerns/src/concern.rb

Overview

TODO: there seems to be a bug when extending classes. They do not get inheritance for free with this module as would be expected.

Instance Method Summary collapse

Instance Method Details

#foobara_on_include_blockObject



39
40
41
# File 'foobara-0.0.110/projects/concerns/src/concern.rb', line 39

def foobara_on_include_block
  @foobara_on_include
end

#has_foobara_on_include_block?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'foobara-0.0.110/projects/concerns/src/concern.rb', line 35

def has_foobara_on_include_block?
  !!@foobara_on_include
end

#included(klass) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'foobara-0.0.110/projects/concerns/src/concern.rb', line 6

def included(klass)
  # If behavior is defined in Concern A and then included into Concern B, we need to make sure
  # that when B is included it's also as if A were included.
  # ClassMethods on A should exist on any object's class that included B.
  # Any code-snippets to be ran when A is included should also be ran if B is included.
  if Concern.foobara_concern?(klass)
    if const_defined?(:ClassMethods, false)
      Concern.foobara_class_methods_module_for(klass).include(const_get(:ClassMethods, false))
    end
  else
    if const_defined?(:ClassMethods, false)
      klass.extend(const_get(:ClassMethods, false))
    end

    ancestors.select { |mod| Concern.foobara_concern?(mod) }.reverse.each do |concern|
      concern.instance_variable_get("@inherited_overridable_class_attr_accessors")&.each do |name|
        var_name = "@#{name}"
        klass.instance_variable_set(var_name, klass.instance_variable_get(var_name))
      end

      if concern.has_foobara_on_include_block?
        klass.class_eval(&concern.foobara_on_include_block)
      end
    end
  end

  super
end

#inherited_overridable_class_attr_accessor(*names) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'foobara-0.0.110/projects/concerns/src/concern.rb', line 47

def inherited_overridable_class_attr_accessor(*names)
  @inherited_overridable_class_attr_accessors ||= []
  @inherited_overridable_class_attr_accessors += names

  Concern.foobara_class_methods_module_for(self).module_eval do
    names.each do |name|
      var_name = "@#{name}"

      define_method name do
        if instance_variable_defined?(var_name)
          instance_variable_get(var_name)
        else
          superclass.send(name)
        end
      end

      attr_writer name
    end
  end
end

#on_include(&block) ⇒ Object



43
44
45
# File 'foobara-0.0.110/projects/concerns/src/concern.rb', line 43

def on_include(&block)
  @foobara_on_include = block
end