Class: Fruity::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/fruity/group.rb

Overview

A group of callable objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Group

Pass either a list of callable objects, a Hash of names and callable objects or an Array of methods names with the option :on specifying which object to call them on (or else the methods are assumed to be global, see the README) Another possibility is to use a block; if it accepts an argument,



14
15
16
17
18
19
# File 'lib/fruity/group.rb', line 14

def initialize(*args, &block)
  @options = DEFAULT_OPTIONS.dup
  @elements = {}
  @counter = 0
  compare(*args, &block)
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



6
7
8
# File 'lib/fruity/group.rb', line 6

def elements
  @elements
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/fruity/group.rb', line 7

def options
  @options
end

Instance Method Details

#compare(*args, &block) ⇒ Object

Adds things to compare. See new for details on interface



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fruity/group.rb', line 23

def compare(*args, &block)
  if args.last.is_a?(Hash) && (args.last.keys - OPTIONS).empty?
    @options.merge!(args.pop)
  end
  case args.first
    when Hash
      raise ArgumentError, "Expected only one hash of {value => executable}, got #{args.size-1} extra arguments" unless args.size == 1
      raise ArgumentError, "Expected values to be executable" unless args.first.values.all?{|v| v.respond_to?(:call)}
      compare_hash(args.first)
    when Symbol, String
      compare_methods(*args)
    else
      compare_lambdas(*args)
  end
  compare_block(block) if block
end

#run(options = {}) ⇒ Object



62
63
64
# File 'lib/fruity/group.rb', line 62

def run(options = {})
  Runner.new(self).run(options)
end

#sizeObject



58
59
60
# File 'lib/fruity/group.rb', line 58

def size
  elements.size
end

#sufficient_magnificationObject

Returns the maximal sufficient_magnification for all elements See Util.sufficient_magnification



43
44
45
# File 'lib/fruity/group.rb', line 43

def sufficient_magnification
  elements.map{|name, exec| Util.sufficient_magnification(exec, options) }.max
end

#sufficient_magnification_and_delayObject

Returns the maximal sufficient_magnification for all elements and the approximate delay taken for the whole group See Util.sufficient_magnification



51
52
53
54
55
56
# File 'lib/fruity/group.rb', line 51

def sufficient_magnification_and_delay
  mags_and_delays = elements.map{|name, exec| Util.sufficient_magnification_and_delay(exec, options) }
  mag = mags_and_delays.map(&:first).max
  delay = mags_and_delays.map{|m, d| d * mag / m}.inject(:+)
  [mag, delay]
end