Class: DoubleEntry::Reporting::TimeRange

Inherits:
Object
  • Object
show all
Defined in:
lib/double_entry/reporting/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.



44
45
46
47
48
# File 'lib/double_entry/reporting/time_range.rb', line 44

def initialize(options)
  @year = options[:year]
  @range_type = options[:range_type] || :normal
  @month = @week = @day = @hour = nil
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



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

def day
  @day
end

#finishObject (readonly)

Returns the value of attribute finish.



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

def finish
  @finish
end

#hourObject (readonly)

Returns the value of attribute hour.



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

def hour
  @hour
end

#monthObject (readonly)

Returns the value of attribute month.



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

def month
  @month
end

#range_typeObject (readonly)

Returns the value of attribute range_type.



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

def range_type
  @range_type
end

#startObject (readonly)

Returns the value of attribute start.



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

def start
  @start
end

#weekObject (readonly)

Returns the value of attribute week.



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

def week
  @week
end

#yearObject (readonly)

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.make(options = {}) ⇒ Object



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

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

.range_from_time_for_period(start_time, period_name) ⇒ Object



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

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

Instance Method Details

#human_readable_nameObject



54
55
56
# File 'lib/double_entry/reporting/time_range.rb', line 54

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

#include?(time) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/double_entry/reporting/time_range.rb', line 39

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

#keyObject



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

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