Class: RiceBubble::Attributes::Number

Inherits:
Base
  • Object
show all
Defined in:
lib/rice_bubble/attributes/number.rb

Direct Known Subclasses

Integer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#call, #fetch, #optional

Constructor Details

#initialize(min: nil, max: nil) ⇒ Number

Returns a new instance of Number.



6
7
8
9
10
# File 'lib/rice_bubble/attributes/number.rb', line 6

def initialize(min: nil, max: nil, &)
  super(&)
  @min = min
  @max = max
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/rice_bubble/attributes/number.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/rice_bubble/attributes/number.rb', line 4

def min
  @min
end

Instance Method Details

#descriptionObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rice_bubble/attributes/number.rb', line 22

def description
  result = super

  if min && max
    "#{result} between #{min} and #{max}"
  elsif min
    "#{result} greater than or equal to #{min}"
  elsif max
    "#{result} less than or equal to #{max}"
  else
    result
  end
end

#valid?(value) ⇒ Boolean

Returns:



16
17
18
19
20
# File 'lib/rice_bubble/attributes/number.rb', line 16

def valid?(value)
  super &&
    !min&.send(:>, value) &&
    !max&.send(:<, value)
end

#valid_typesObject



12
13
14
# File 'lib/rice_bubble/attributes/number.rb', line 12

def valid_types
  [::Numeric]
end