Class: Ordinary::Unit

Inherits:
Object
  • Object
show all
Includes:
Composable
Defined in:
lib/ordinary/unit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#<<, #>>, #instance_id, #owned?, #owned_by, #owner

Constructor Details

#initialize(original_unit, requirements = nil, arguments = [], &process) ⇒ Unit

Returns a new instance of Unit.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
# File 'lib/ordinary/unit.rb', line 33

def initialize(original_unit, requirements = nil, arguments = [], &process)
  raise ArgumentError, 'block not supplied' unless block_given?
  @original_unit = original_unit
  @requirements  = requirements
  @arguments     = arguments
  @process       = process
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



43
44
45
# File 'lib/ordinary/unit.rb', line 43

def arguments
  @arguments
end

#processObject (readonly)

Returns the value of attribute process.



45
46
47
# File 'lib/ordinary/unit.rb', line 45

def process
  @process
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



41
42
43
# File 'lib/ordinary/unit.rb', line 41

def requirements
  @requirements
end

Instance Method Details

#inspectObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/ordinary/unit.rb', line 57

def inspect
  original_owner = ''

  if @original_unit
    argument_list  = @arguments.map(&:inspect) * ', '
    original_owner = " (#{@original_unit.owner} with [#{argument_list}])"
  end

  "#<#{instance_id} #{owner}#{original_owner}>"
end

#to_procObject



51
52
53
54
55
# File 'lib/ordinary/unit.rb', line 51

def to_proc
  @requirements.load if @requirements and !@requirements.loaded?
  args = @arguments + (@original_unit ? @original_unit.arguments : [])
  lambda { |value| @process.call(value, *args) }
end

#with(*arguments) ⇒ Object



47
48
49
# File 'lib/ordinary/unit.rb', line 47

def with(*arguments)
  self.class.new(self, @requirements, arguments, &@process)
end