Class: Procedo::Cardinality

Inherits:
Object
  • Object
show all
Defined in:
lib/procedo/cardinality.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Cardinality

Returns a new instance of Cardinality.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/procedo/cardinality.rb', line 5

def initialize(object)
  if object.is_a?(Cardinality)
    @minimum = object.minimum if object.minimum?
    @maximum = object.maximum if object.maximum?
  elsif object.is_a?(String)
    if object == '+'
      @minimum = 1
    elsif object == '?'
      @maximum = 1
    elsif object =~ /\A(\d+)?\.\.(\d+)?\z/
      array = object.split('..').map(&:strip)
      @minimum = array.first.to_i if array.first.present?
      @maximum = array.second.to_i if array.second.present?
    elsif object =~ /\A\d+\z/
      @maximum = object.to_i
      @minimum = object.to_i
    elsif object != '*'
      raise "Cannot parse that: #{object.inspect}"
    end
  elsif object.is_a?(Numeric)
    @minimum = object.to_i
    @maximum = object.to_i
  elsif object.is_a?(Range)
    @minimum = array.min
    @maximum = array.max
  else
    raise "Cannot handle that: #{object.inspect}"
  end
end

Instance Attribute Details

#maximumObject

Returns the value of attribute maximum


3
4
5
# File 'lib/procedo/cardinality.rb', line 3

def maximum
  @maximum
end

#minimumObject

Returns the value of attribute minimum


3
4
5
# File 'lib/procedo/cardinality.rb', line 3

def minimum
  @minimum
end

Instance Method Details

#==(other_cardinality) ⇒ Object


62
63
64
65
# File 'lib/procedo/cardinality.rb', line 62

def ==(other_cardinality)
  other = self.class.new(other_cardinality)
  minimum == other.minimum && maximum == other.maximum
end

#include?(number) ⇒ Boolean

Returns true if number is in the range, false otherwise.

Returns:

  • (Boolean)

36
37
38
39
40
# File 'lib/procedo/cardinality.rb', line 36

def include?(number)
  return false if minimum? && @minimum > number
  return false if maximum? && @maximum < number
  true
end

#maximum?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/procedo/cardinality.rb', line 58

def maximum?
  @maximum.present?
end

#minimum?Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/procedo/cardinality.rb', line 54

def minimum?
  @minimum.present?
end