Module: AsRange::ClassMethods

Defined in:
lib/as_range.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  start: :start_date,
  end: :end_date,
  method_name: :as_range,
  include_end: true
}.freeze

Instance Method Summary collapse

Instance Method Details

#as_range(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/as_range.rb', line 18

def as_range(options = {})
  options = DEFAULT_OPTIONS.merge(options)
  start_attribute = options[:start]
  end_attribute = options[:end]

  define_method options[:method_name] do
    start_value = start_attribute.call if start_attribute.respond_to?(:call)
    start_value ||= public_send(start_attribute)

    end_value = end_attribute.call if end_attribute.respond_to?(:call)
    end_value ||= public_send(end_attribute)

    Range.new(start_value, end_value, !options[:include_end])
  end
end