Class: ActsAsCalculator::Tier

Inherits:
Data
  • Object
show all
Defined in:
lib/acts_as_calculator/tier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from

Returns:

  • (Object)

    the current value of from



4
5
6
# File 'lib/acts_as_calculator/tier.rb', line 4

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to

Returns:

  • (Object)

    the current value of to



4
5
6
# File 'lib/acts_as_calculator/tier.rb', line 4

def to
  @to
end

#valueObject (readonly)

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



4
5
6
# File 'lib/acts_as_calculator/tier.rb', line 4

def value
  @value
end

Class Method Details

.build(source) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/acts_as_calculator/tier.rb', line 5

def self.build(source)
  return source if source.is_a?(Tier)

  attributes = source.respond_to?(:to_h) ? source.to_h : read_from(source)
  attributes = attributes.transform_keys(&:to_sym)

  new(from: attributes[:from], to: attributes[:to], value: attributes[:value])
end

Instance Method Details

#covers?(amount) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/acts_as_calculator/tier.rb', line 19

def covers?(amount)
  amount >= lower_bound && amount < upper_bound
end

#lower_boundObject



23
24
25
# File 'lib/acts_as_calculator/tier.rb', line 23

def lower_bound
  from || -Float::INFINITY
end

#upper_boundObject



27
28
29
# File 'lib/acts_as_calculator/tier.rb', line 27

def upper_bound
  to || Float::INFINITY
end