Module: Kinda::Core::ModuleExtension

Included in:
Module
Defined in:
lib/core/module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
# File 'lib/core/module.rb', line 4

def self.included(klass)
  klass.alias_method_chain :append_features, :inheritable_extensions
end

Instance Method Details

#alias_method_chain(target, feature) {|aliased_target, punctuation| ... } ⇒ Object

Yields:

  • (aliased_target, punctuation)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/core/module.rb', line 8

def alias_method_chain(target, feature)
  # Strip out punctuation on predicates or bang methods since
  # e.g. target?_without_feature is not a valid method name.
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?

  with_method, without_method =
    "#{aliased_target}_with_#{feature}#{punctuation}",
    "#{aliased_target}_without_#{feature}#{punctuation}"

  alias_method without_method, target
  alias_method target, with_method

  case
    when public_method_defined?(without_method)
      public target
    when protected_method_defined?(without_method)
      protected target
    when private_method_defined?(without_method)
      private target
  end
end

#ancestorObject



83
84
85
# File 'lib/core/module.rb', line 83

def ancestor
  ancestors_without_self.first
end

#ancestors_without_selfObject



78
79
80
81
# File 'lib/core/module.rb', line 78

def ancestors_without_self
  result = ancestors
  result.first == self ? result.drop(1) : result
end

#append_features_with_inheritable_extensions(target) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/core/module.rb', line 52

def append_features_with_inheritable_extensions(target)
  append_features_without_inheritable_extensions(target)
  if @inheritable_extensions
    target.extend *@inheritable_extensions
    target.inheritable_extend *@inheritable_extensions.reverse
  end
end

#containerObject



31
32
33
34
# File 'lib/core/module.rb', line 31

def container
  space = name[0...(name.rindex( '::' ) || 0)]
  space.empty? ? Object : eval(space)
end

#delegate_to_class(*sources) ⇒ Object

delegate_to_class :singleton_method_added => :method_added delegate_to_class [:attr_reader, :attr_writer] delegate_to_class ClassMethods.instance_methods



63
64
65
66
67
68
69
70
# File 'lib/core/module.rb', line 63

def delegate_to_class(*sources)
  sources, target = sources.first.first if sources.first.is_a?(Hash)
  [*sources].flatten.each do |source|
    define_method(source) do |*args, &block|
      singleton_class_send(target || source, *args, &block)
    end
  end
end

#extensionsObject



36
37
38
# File 'lib/core/module.rb', line 36

def extensions
  singleton_class.ancestors.take_while { |mod| !mod.is_a?(Class) }
end

#inheritable_extend(*mods, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/core/module.rb', line 40

def inheritable_extend(*mods, &block)
  if block
    mod = Module.new
    mod.extend extensions
    mod.module_eval(&block)
    mods << mod
  end
  extend *mods
  (@inheritable_extensions ||= []).unshift(*mods.reverse).uniq!
  mod
end

#self_and_ancestorsObject



72
73
74
75
76
# File 'lib/core/module.rb', line 72

def self_and_ancestors
  result = ancestors
  result.unshift self unless result.first == self
  result
end