Class: Module

Inherits:
Object show all
Defined in:
lib/y_support/unicode.rb,
lib/y_support/core_ext/module/misc.rb,
lib/y_support/typing/module/typing.rb

Overview

y_support/unicode also defines the following aliases:

  • – alias for include

  • ç for class in several method names

Instance Method Summary collapse

Instance Method Details

#chain(**hash, &block) ⇒ Object

Defines a set of methods by applying the block on the return value of another set of methods. Accepts a hash of pairs { mapped_method_symbol => original_method_symbol } and a block which to chain to the original method result.



63
64
65
66
67
68
69
# File 'lib/y_support/core_ext/module/misc.rb', line 63

def chain **hash, &block
  hash.each_pair { |mapped_method_symbol, original_method_symbol|
    define_method mapped_method_symbol do |*args, &b|
      block.( send original_method_symbol, *args, &b )
    end
  }
end

#complianceObject

Compliance (declared compliance + ancestors).



19
20
21
# File 'lib/y_support/typing/module/typing.rb', line 19

def compliance
  ( declared_compliance + ancestors ).uniq
end

#complies?(other_module) ⇒ Boolean

Compliance inquirer (declared compliance + ancestors).

Returns:

  • (Boolean)


6
7
8
# File 'lib/y_support/typing/module/typing.rb', line 6

def complies?( other_module )
  compliance.include? other_module.aT_kind_of( Module, "other module" )
end

#const_reset!(const, value) ⇒ Object

Redefines a constant without warning.



53
54
55
56
# File 'lib/y_support/core_ext/module/misc.rb', line 53

def const_reset!( const, value )
  send :remove_const, const if const_defined? const
  const_set( const, value )
end

#const_set_if_not_defined(const, value) ⇒ Object

Sets a constant to a value if this has not been previously defined.



47
48
49
# File 'lib/y_support/core_ext/module/misc.rb', line 47

def const_set_if_not_defined( const, value )
  const_set( const, value ) unless const_defined? const
end

#declare_compliance!(other_module) ⇒ Object

Using this method, the receiver explicitly declares that its interface complies with another module (class).



34
35
36
37
38
39
# File 'lib/y_support/typing/module/typing.rb', line 34

def declare_compliance! other_module
  other_module.aT_kind_of Module, "other module"
  return false if declared_compliance.include? other_module
  ( @declared_compliance ||= [] ) << other_module
  return true
end

#declared_complianceObject

Declared compliance getter.



25
26
27
28
29
# File 'lib/y_support/typing/module/typing.rb', line 25

def declared_compliance
  ( ( @declared_compliance || [] ) + ancestors.map { |a|
      a.instance_variable_get( :@declared_compliance ) || []
    }.reduce( [], :+ ) ).uniq
end

#declares_compliance?(other_module) ⇒ Boolean

Declared compliance inquirer.

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/y_support/typing/module/typing.rb', line 12

def declares_compliance?( other_module )
  other_module.aT_kind_of Module, "other module"
  declared_compliance.include? other_module
end

#heir_class(mother = Object, **parameters, &block) ⇒ Object

Creates a class, which is a subclass of a supplied class (defaults to Object if none supplied), and which also inherits from the receiver and is parametrized by the given set of parameters. The parameters have form { symbol: value } and they cause singleton method(s) named “symbol” be defined on the heir class, returning “value”.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/y_support/core_ext/module/misc.rb', line 33

def heir_class mother=Object, **parameters, &block
  s = self
  Class.new( mother ) { include s }.tap do |c|
    parameters.each_pair { |symbol, value|
      c.define_singleton_method symbol do value end
    }
    # TODO: This line is controversial:
    c.define_singleton_method inspect do s.inspect + "<" end
    c.module_exec &block if block
  end
end

#heir_module(**parameters, &block) ⇒ Object

Creates a module that inherits from the receiver and is parametrized with the given set of parameters. The parameters have form { symbol: value } and they cause singleton method(s) named “symbol” be defined on the heir, returning “value”.



16
17
18
19
20
21
22
23
24
25
# File 'lib/y_support/core_ext/module/misc.rb', line 16

def heir_module **parameters, &block
  s = self
  Module.new { include s }.tap do |m|
    parameters.each_pair { |symbol, value|
      m.define_singleton_method symbol do value end
    }
    m.define_singleton_method inspect do s.inspect + "<" end
    m.module_exec &block if block
  end
end