Class: Value::Values

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/value/values.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ Values

Returns a new instance of Values.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/value/values.rb', line 6

def initialize(*values)
  options = Hash === values.last ? values.pop : {}
  values.pop if @block = values.last.to_s.start_with?('&') ? values.last.to_s[1..-1].to_sym : nil
  values.pop if @splat = (values.last and values.last.to_s.start_with?('*')) ?
  values.last.to_s[1..-1].to_sym : nil
  required, optional = values.partition{ |e| not(Array === e) }
  @required = required.map(&:to_sym)
  @optional = optional.map{ |e| [e.first.to_sym, e.last] }
  all = to_a
  @comparable = Array === options[:comparable] ?
    options[:comparable].each{ |e|
      raise ArgumentError, '%p is not among comparable members %s' % [e, all.map(&:inspect).join(', ')] unless all.include?(e)
    } :
    all
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



43
44
45
# File 'lib/value/values.rb', line 43

def block
  @block
end

#comparableObject (readonly)

Returns the value of attribute comparable.



43
44
45
# File 'lib/value/values.rb', line 43

def comparable
  @comparable
end

#optionalObject (readonly)

Returns the value of attribute optional.



43
44
45
# File 'lib/value/values.rb', line 43

def optional
  @optional
end

#requiredObject (readonly)

Returns the value of attribute required.



43
44
45
# File 'lib/value/values.rb', line 43

def required
  @required
end

#splatObject (readonly)

Returns the value of attribute splat.



43
44
45
# File 'lib/value/values.rb', line 43

def splat
  @splat
end

Instance Method Details

#==(other) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/value/values.rb', line 35

def ==(other)
  self.class == other.class and
    required == other.required and
    optional == other.optional and
    splat == other.splat and
    block == other.block
end

#each {|splat| ... } ⇒ Object

Yields:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/value/values.rb', line 22

def each
  return enum_for(__method__) unless block_given?
  required.each do |value|
    yield value
  end
  optional.each do |value, _|
    yield value
  end
  yield splat if splat
  yield block if block
  self
end