Class: DoubleEntry::TimeRange

Inherits:
Object
  • Object
show all
Defined in:
lib/double_entry/time_range.rb

Direct Known Subclasses

DayRange, HourRange, MonthRange, WeekRange, YearRange

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TimeRange

Returns a new instance of TimeRange.



42
43
44
45
# File 'lib/double_entry/time_range.rb', line 42

def initialize(options)
  @year = options[:year]
  @range_type = options[:range_type] || :normal
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def day
  @day
end

#finishObject (readonly)

Returns the value of attribute finish.



4
5
6
# File 'lib/double_entry/time_range.rb', line 4

def finish
  @finish
end

#hourObject (readonly)

Returns the value of attribute hour.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def hour
  @hour
end

#monthObject (readonly)

Returns the value of attribute month.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def month
  @month
end

#range_typeObject (readonly)

Returns the value of attribute range_type.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def range_type
  @range_type
end

#startObject (readonly)

Returns the value of attribute start.



4
5
6
# File 'lib/double_entry/time_range.rb', line 4

def start
  @start
end

#weekObject (readonly)

Returns the value of attribute week.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def week
  @week
end

#yearObject (readonly)

Returns the value of attribute year.



5
6
7
# File 'lib/double_entry/time_range.rb', line 5

def year
  @year
end

Class Method Details

.make(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/double_entry/time_range.rb', line 7

def self.make(options = {})
  @options = options
  case
    when (options[:year] and options[:week] and options[:day] and options[:hour])
      HourRange.new(options)
    when (options[:year] and options[:week] and options[:day])
      DayRange.new(options)
    when (options[:year] and options[:week])
      WeekRange.new(options)
    when (options[:year] and options[:month])
      MonthRange.new(options)
    when options[:year]
      YearRange.new(options)
    else
      raise "Invalid range information #{options}"
  end
end

.range_from_time_for_period(start_time, period_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/double_entry/time_range.rb', line 25

def self.range_from_time_for_period(start_time, period_name)
   case period_name
     when 'month'
       DoubleEntry::YearRange.from_time(start_time)
     when 'week'
        DoubleEntry::YearRange.from_time(start_time)
     when 'day'
       DoubleEntry::MonthRange.from_time(start_time)
     when 'hour'
       DoubleEntry::DayRange.from_time(start_time)
   end
end

Instance Method Details

#human_readable_nameObject



51
52
53
# File 'lib/double_entry/time_range.rb', line 51

def human_readable_name
  self.class.name.gsub('DoubleEntry::', '').gsub('Range', '')
end

#include?(time) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/double_entry/time_range.rb', line 38

def include?(time)
  (time >= @start) and (time <= @finish)
end

#keyObject



47
48
49
# File 'lib/double_entry/time_range.rb', line 47

def key
  "#{@year}:#{@month}:#{@week}:#{@day}:#{@hour}"
end