Module: Ordinary::Module

Defined in:
lib/ordinary/module.rb

Defined Under Namespace

Classes: Requirements

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#requirementsOrdinary::Module::Requirements (readonly)

Returns libraries that the module requires.

Returns:



10
11
12
# File 'lib/ordinary/module.rb', line 10

def requirements
  @requirements ||= Requirements.new
end

Instance Method Details

#requires(*libraries) ⇒ Object

Add libraries to the requirements.

Parameters:

  • libraries (Array<String>)

    required libraries

See Also:



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

def requires(*libraries)
  requirements.add(*libraries)
end

#unit(name, unit = nil) {|value, *args| ... } ⇒ Object

Define an unit for some normalizer.

Examples:

define an unit with a block


unit :lstrip do |value|
  value.lstrip
end

# same as above
unit :lstrip, lambda { |value| value.lstrip }

define an unit simply


# call .lstrip of a value
unit :lstrip

# and named "ltrim"
unit :ltrim, :lstrip

define an unit with existing units


unit :lstrip
unit :rstrip

# use an existing unit
unit :ltrim, lstrip
unit :rtrim, rstrip

# use by combining existing units (by #|, #>> or #<<)
unit :trim, ltrim | rtrim

Parameters:

  • name (Symbol)

    name of the unit

  • unit (Symbol) (defaults to: nil)
  • unit (Proc) (defaults to: nil)

    process of normalization that the unit plays

  • unit (Ordinary::Unit, Ordinary::Units) (defaults to: nil)

    an existing unit or combination existing units

Yields:

  • (value, *args)

    process of normalization that the unit plays

Yield Parameters:

  • value (Object)

    a value to process

  • args (Array<Object>)

    additional arguments



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ordinary/module.rb', line 62

def unit(name, unit = nil, &block)
  unit = unit.to_sym if unit.is_a?(String)

  unit = if unit.nil? and block.nil?
           unit_by_send(name)
         elsif unit.is_a?(Symbol)
           unit_by_send(unit)
         elsif unit.is_a?(Proc)
           create_unit(&unit)
         elsif block_given?
           create_unit(&block)
         else
           unit
         end

  unit.owned_by(self, name) unless unit.owned?
  define_method(name) { |*args| args.empty? ? unit : unit.with(*args) }
  module_function name
end