Class: Redlics::TimeFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/redlics/time_frame.rb

Overview

Time Frame class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, time_object, options = {}) ⇒ Redlics::TimeFrame

Initialization of a time frame object.

Parameters:

  • context (Hash)

    the hash of a context defined in Redlics::CONTEXTS

  • time_object (Symbol)

    time object predefined in Redlics::TimeFrame.init_with_symbol

  • time_object (Hash)

    time object with keys ‘from` and `to`

  • time_object (Range)

    time object as range

  • time_object (Time)

    time object

  • options (Hash) (defaults to: {})

    configuration options

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/redlics/time_frame.rb', line 18

def initialize(context, time_object, options = {})
  raise ArgumentError, 'TimeFrame should be initialized with Symbol, Hash, Range or Time' unless [Symbol, Hash, Range, Time].include?(time_object.class)
  @from, @to = self.send("init_with_#{time_object.class.name.demodulize.underscore}", time_object, context)
  @granularity = Granularity.validate(context, options[:granularity]).first
end

Instance Attribute Details

#fromObject (readonly)

Gives read access to the listed instance variables.



7
8
9
# File 'lib/redlics/time_frame.rb', line 7

def from
  @from
end

#granularityObject (readonly)

Gives read access to the listed instance variables.



7
8
9
# File 'lib/redlics/time_frame.rb', line 7

def granularity
  @granularity
end

#toObject (readonly)

Gives read access to the listed instance variables.



7
8
9
# File 'lib/redlics/time_frame.rb', line 7

def to
  @to
end

Instance Method Details

#splatArray

Construct keys by time frame steps.

Returns:

  • (Array)

    keys



26
27
28
29
30
31
32
# File 'lib/redlics/time_frame.rb', line 26

def splat
  [].tap do |keys|
    (from.to_i .. to.to_i).step(Redlics.config.granularities[@granularity][:step]) do |t|
      keys << (block_given? ? (yield Time.at(t)) : Time.at(t))
    end
  end
end