Class: UniqSysOmega::Tariffs::RepeatingFilterPeriod

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starts_at, length, period, filter) ⇒ RepeatingFilterPeriod

Returns a new instance of RepeatingFilterPeriod.



23
24
25
26
27
28
29
# File 'lib/repeating_filter.rb', line 23

def initialize(starts_at, length, period, filter)
  raise "period should be more than length, but this assertion is not met: #{length} <= #{period}" if length > period
  @starts_at = starts_at.to_i
  @length = length
  @period = period
  @filter = filter
end

Instance Attribute Details

#filterObject

Returns the value of attribute filter.



21
22
23
# File 'lib/repeating_filter.rb', line 21

def filter
  @filter
end

#lengthObject

Returns the value of attribute length.



19
20
21
# File 'lib/repeating_filter.rb', line 19

def length
  @length
end

#periodObject

Returns the value of attribute period.



20
21
22
# File 'lib/repeating_filter.rb', line 20

def period
  @period
end

#starts_atObject

Returns the value of attribute starts_at.



18
19
20
# File 'lib/repeating_filter.rb', line 18

def starts_at
  @starts_at
end

Class Method Details

.from_hash(hash, filter = nil) ⇒ Object



31
32
33
# File 'lib/repeating_filter.rb', line 31

def self.from_hash(hash, filter=nil)
  self.new(hash[:starts_at], hash[:length], hash[:period], filter || hash[:filter])
end

Instance Method Details

#===(value) ⇒ Object



35
36
37
# File 'lib/repeating_filter.rb', line 35

def ===(value)
  includes?(value)
end

#generate_in_range(start, finish) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/repeating_filter.rb', line 50

def generate_in_range(start, finish)
  start, finish = start.to_i, finish.to_i
  [ ].tap do |ranges|
    local_period_start = @starts_at + ((start-@starts_at).to_f/@period.to_f).ceil * @period
    while local_period_start+@length <= finish
      ranges << SortableRange.new(local_period_start, local_period_start+@length, true).tap { |r| r.filter = filter }
      local_period_start += @period
    end
  end
end

#includes?(value) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/repeating_filter.rb', line 46

def includes?(value)
  local_period(value).include?(value.to_i)
end

#local_period(value) ⇒ Object



39
40
41
42
43
44
# File 'lib/repeating_filter.rb', line 39

def local_period(value)
  local_period_start = @starts_at + ((value.to_i-@starts_at)/@period).floor * @period
  SortableRange.new(local_period_start, local_period_start+@length, true).tap do |range|
    range.filter = filter
  end
end

#to_sObject



61
62
63
# File 'lib/repeating_filter.rb', line 61

def to_s
  "RepeatingFilterPeriod(starts_at=#{starts_at}, length=#{length}, period=#{period})"
end