module Ordinary::Module

Public Instance Methods

requirements() click to toggle source

@attribute [r] requirements @return [Ordinary::Module::Requirements] libraries that the module

requires
# File lib/ordinary/module.rb, line 10
def requirements
  @requirements ||= Requirements.new
end
requires(*libraries) click to toggle source

Add libraries to the requirements.

@param [Array<String>] libraries required libraries

@see Ordinary::Module#requirements

# File lib/ordinary/module.rb, line 19
def requires(*libraries)
  requirements.add(*libraries)
end
unit(name, unit = nil, &block) click to toggle source

Define an unit for some normalizer.

@example define an unit with a block

unit :lstrip do |value|
  value.lstrip
end

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

@example define an unit simply

# call .lstrip of a value
unit :lstrip

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

@example 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

@param [Symbol] name name of the unit @param [Symbol] unit @param [Proc] unit process of normalization that the unit plays @param [Ordinary::Unit, Ordinary::Units] unit an existing unit or

combination existing units

@yield [value, *args] process of normalization that the unit plays @yieldparam [Object] value a value to process @yieldparam [Array<Object>] args additional arguments

# 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

Private Instance Methods

create_unit(&process) click to toggle source
# File lib/ordinary/module.rb, line 88
def create_unit(&process)
  Unit.new(nil, requirements, &process)
end
unit_by_send(method_name) click to toggle source
# File lib/ordinary/module.rb, line 84
def unit_by_send(method_name)
  create_unit { |value, *args| value.__send__(method_name, *args) }
end