class RSS::Rule

Class to build and compare RSS Rules.

Attributes

duration[R]

getters

id[R]

getters

recurrence[R]

Public Class Methods

dump(rules) click to toggle source

@return JSON string of rules

# File lib/rss/rule.rb, line 125
def self.dump(rules)
  value = rules.is_a?(Array) ? rules.map(&:as_json) : []
  JSON.dump(value)
end
load(value) click to toggle source

@return [Array of Rule Objects]

# File lib/rss/rule.rb, line 115
def self.load(value)
  parsed = JSON.load(value)
  parsed.is_a?(Array) ? parsed.map { |p| new(p) } : []
end
new(attributes = {}) click to toggle source
# File lib/rss/rule.rb, line 6
def initialize(attributes = {})
  @recurrence = {}

  self.id         = SecureRandom.uuid
  self.attributes = attributes
end

Public Instance Methods

==(other) click to toggle source
# File lib/rss/rule.rb, line 120
def ==(other)
  as_json == other.as_json
end
as_json(_options = nil) click to toggle source

@return JSON string

# File lib/rss/rule.rb, line 96
def as_json(_options = nil)
  # when on rails 4 use deep stringify keys
  attributes.stringify_keys
end
attributes() click to toggle source
# File lib/rss/rule.rb, line 36
def attributes
  {}.tap do |a|
    a[:id] = id
    a[:recurrence] = @recurrence.stringify_keys
    a[:duration]   = @duration if @duration
  end
end
attributes=(attributes = {}) click to toggle source
# File lib/rss/rule.rb, line 87
def attributes=(attributes = {})
  attributes.each_pair do |attr, value|
    if respond_to?(assign = "#{attr}=")
      send(assign, value)
    end
  end
end
convert_date(t) click to toggle source
# File lib/rss/rule.rb, line 44
def convert_date(t)
  t = t.is_a?(String) ? Time.zone.parse(t) : t
  return if t.nil?
  t.utc.iso8601
end
count() click to toggle source
# File lib/rss/rule.rb, line 24
def count
  @recurrence[:count]
end
count=(c) click to toggle source
# File lib/rss/rule.rb, line 55
def count=(c)
  @recurrence[:count] = c
end
duration=(d) click to toggle source
# File lib/rss/rule.rb, line 75
def duration=(d)
  @duration = d.to_i
end
ends_at() click to toggle source
# File lib/rss/rule.rb, line 20
def ends_at
  @recurrence[:ends_at]
end
ends_at=(t) click to toggle source
# File lib/rss/rule.rb, line 71
def ends_at=(t)
  @recurrence[:ends_at] = convert_date(t)
end
frequency() click to toggle source
# File lib/rss/rule.rb, line 28
def frequency
  @recurrence[:frequency] || 'daily'
end
frequency=(f) click to toggle source
# File lib/rss/rule.rb, line 59
def frequency=(f)
  @recurrence[:frequency] = f.to_s
end
id=(id) click to toggle source

setters

# File lib/rss/rule.rb, line 51
def id=(id)
  @id = id.to_s
end
interval() click to toggle source
# File lib/rss/rule.rb, line 32
def interval
  @recurrence[:interval] || 1
end
interval=(i) click to toggle source
# File lib/rss/rule.rb, line 63
def interval=(i)
  @recurrence[:interval]  = i.to_i
end
one_off?() click to toggle source

Tell wether self is an one-off Rule or not. self is considered to be one-off if and only if it evaluates to a single occurrence.

@return [true] if self is a one-off Rule. This is 100% reliable. @return [false] if self looks like it is not an one-off Rule. This is

not 100% reliable. Unfortunately, there is no obvious way to tell for
sure if +self+ evaluates to multple occurrences or not.
# File lib/rss/rule.rb, line 108
def one_off?
  # TODO@cs: There are various problematic edge-cases in the system, because
  # +#one_off?+ is not 100% reliable. We have to fix this ASAP.
  count == 1 || starts_at == ends_at
end
recurrence=(r) click to toggle source
# File lib/rss/rule.rb, line 79
def recurrence=(r)
  if r.is_a?(Hash)
    self.attributes = r
  else
    fail "recurrence= can only receive a Hash. Received #{r.class}"
  end
end
starts_at() click to toggle source
# File lib/rss/rule.rb, line 16
def starts_at
  @recurrence[:starts_at]
end
starts_at=(t) click to toggle source
# File lib/rss/rule.rb, line 67
def starts_at=(t)
  @recurrence[:starts_at] = convert_date(t)
end