Module: Module::Mixy

Defined in:
lib/module/mixy.rb

Defined Under Namespace

Classes: ConflictError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check_conflicts(specific_module) ⇒ nil (private)

Returns:

  • (nil)

58
59
60
61
62
63
64
65
66
67
# File 'lib/module/mixy.rb', line 58

def check_conflicts(specific_module)
  conflicts = methods_from(self) & methods_from(specific_module)
  conflicts.each do |conflict|
    err = ConflictError.new \
          "#{conflict} is a conflict name - all coflicts: [#{conflicts.join(', ')}]", conflict
    raise err
  end

  nil
end

.methods_from(mod) ⇒ Array<Symbol>

Parameters:

Returns:

  • (Array<Symbol>)

27
28
29
# File 'lib/module/mixy.rb', line 27

def methods_from(mod)
  mod.instance_methods(true) | mod.private_instance_methods(true)
end

.specific_module_from(feature_module, aliases) ⇒ Module (private)

Returns:


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/module/mixy.rb', line 34

def specific_module_from(feature_module, aliases)
  specific_module = feature_module.dup
  keep_features = methods_from self

  specific_module.module_eval do
    features = instance_methods(false) | private_instance_methods(false)
    ignores = Mixy.methods_from(specific_module) - (keep_features | features | aliases.keys)
    undef_method(*ignores)

    aliases.each_pair do |origin, aliased|
      alias_method aliased, origin if aliased

      if keep_features.include? origin
        remove_method origin
      else
        undef_method origin
      end
    end
  end

  specific_module
end

Instance Method Details

#check_conflicts(specific_module) ⇒ nil (private)

Returns:

  • (nil)

58
59
60
61
62
63
64
65
66
67
# File 'lib/module/mixy.rb', line 58

def check_conflicts(specific_module)
  conflicts = methods_from(self) & methods_from(specific_module)
  conflicts.each do |conflict|
    err = ConflictError.new \
          "#{conflict} is a conflict name - all coflicts: [#{conflicts.join(', ')}]", conflict
    raise err
  end

  nil
end

#methods_from(mod) ⇒ Array<Symbol> (private)

Parameters:

Returns:

  • (Array<Symbol>)

27
28
29
# File 'lib/module/mixy.rb', line 27

def methods_from(mod)
  mod.instance_methods(true) | mod.private_instance_methods(true)
end

#mixy(feature_module, aliases = {}) ⇒ self

if passes `aliases`, defines the renamed methods. if aliases are not given, behaves as `include` with preventing to conflict.

Parameters:

  • feature_module (Module)
  • aliases (Hash) (defaults to: {})

    origin<Symbol> => aliased<Symbol,nil,false>

Returns:

  • (self)

15
16
17
18
19
20
21
# File 'lib/module/mixy.rb', line 15

def mixy(feature_module, aliases={})
  specific_module = specific_module_from feature_module, aliases
  check_conflicts specific_module
  
  include specific_module
  self
end

#specific_module_from(feature_module, aliases) ⇒ Module (private)

Returns:


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/module/mixy.rb', line 34

def specific_module_from(feature_module, aliases)
  specific_module = feature_module.dup
  keep_features = methods_from self

  specific_module.module_eval do
    features = instance_methods(false) | private_instance_methods(false)
    ignores = Mixy.methods_from(specific_module) - (keep_features | features | aliases.keys)
    undef_method(*ignores)

    aliases.each_pair do |origin, aliased|
      alias_method aliased, origin if aliased

      if keep_features.include? origin
        remove_method origin
      else
        undef_method origin
      end
    end
  end

  specific_module
end