Module: PriceHubble::EntityConcern::Attributes::Range

Extended by:
ActiveSupport::Concern
Included in:
PriceHubble::EntityConcern::Attributes
Defined in:
lib/pricehubble/entity/concern/attributes/range.rb

Overview

A separated range typed attribute helper.

Class Method Summary collapse

Class Method Details

.typed_attr_range(name, **_args) ⇒ Object

Register a range attribute.

Parameters:

  • name (Symbol, String)

    the name of the attribute

  • _args (Hash{Symbol => Mixed})

    additional options



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pricehubble/entity/concern/attributes/range.rb', line 15

def typed_attr_range(name, **_args)
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      # We cannot handle nil values
      return unless @#{name}

      # Otherwise we assume the hash contains a +lower+ and +upper+
      # key from which we assemble the range
      hash = @#{name}
      hash[:lower]..hash[:upper]
    end
  RUBY
end