Module: StateOfTheNation::ClassMethods

Defined in:
lib/state_of_the_nation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finish_keyObject

Returns the value of attribute finish_key.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def finish_key
  @finish_key
end

#ignore_emptyObject

Returns the value of attribute ignore_empty.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def ignore_empty
  @ignore_empty
end

#parent_associationObject

Returns the value of attribute parent_association.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def parent_association
  @parent_association
end

#prevent_multiple_activeObject

Returns the value of attribute prevent_multiple_active.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def prevent_multiple_active
  @prevent_multiple_active
end

#start_keyObject

Returns the value of attribute start_key.



16
17
18
# File 'lib/state_of_the_nation.rb', line 16

def start_key
  @start_key
end

Instance Method Details

#considered_active(ignore_empty: false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/state_of_the_nation.rb', line 18

def considered_active(ignore_empty: false)
  @ignore_empty = ignore_empty

  def from(start_key)
    @start_key = start_key
    self
  end

  def until(finish_key)
    @finish_key = finish_key

    define_method "active?" do |time = Time.now.utc|
      (finish.blank? || finish > time) && start <= time
    end

    define_method "active_in_interval?" do |interval_start, interval_end|
      record_start = start
      record_end = finish
      if ignore_empty && record_start == record_end
        false
      elsif interval_start.nil? && interval_end.nil?
        true
      elsif interval_start == interval_end
        active?(interval_start)
      elsif interval_start.nil?
        record_start < interval_end
      elsif interval_end.nil?
        record_end.nil? || record_end > interval_start
      elsif record_end.nil?
        interval_end > record_start
      else
        record_start < interval_end && record_end > interval_start
      end
    end

    scope :active, lambda { |time = Time.now.utc|
      where(QueryString.query_for(:active_scope, self), time, time)
    }
  end

  self
end

#from(start_key) ⇒ Object



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

def from(start_key)
  @start_key = start_key
  self
end

#has_active(association_plural) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/state_of_the_nation.rb', line 61

def has_active(association_plural)
  @association_plural = association_plural
  add_child_methods(plural: @association_plural, single: false, with_identity_cache: false)

  def with_identity_cache
    add_child_methods(plural: @association_plural, single: false, with_identity_cache: true)
    self
  end

  self
end

#has_uniquely_active(association_singular) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/state_of_the_nation.rb', line 73

def has_uniquely_active(association_singular)
  @association_plural = association_singular.to_s.pluralize
  add_child_methods(plural: @association_plural, single: true, with_identity_cache: false)

  def with_identity_cache
    add_child_methods(plural: @association_plural, single: true, with_identity_cache: true)
    self
  end

  self
end

#until(finish_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/state_of_the_nation.rb', line 26

def until(finish_key)
  @finish_key = finish_key

  define_method "active?" do |time = Time.now.utc|
    (finish.blank? || finish > time) && start <= time
  end

  define_method "active_in_interval?" do |interval_start, interval_end|
    record_start = start
    record_end = finish
    if ignore_empty && record_start == record_end
      false
    elsif interval_start.nil? && interval_end.nil?
      true
    elsif interval_start == interval_end
      active?(interval_start)
    elsif interval_start.nil?
      record_start < interval_end
    elsif interval_end.nil?
      record_end.nil? || record_end > interval_start
    elsif record_end.nil?
      interval_end > record_start
    else
      record_start < interval_end && record_end > interval_start
    end
  end

  scope :active, lambda { |time = Time.now.utc|
    where(QueryString.query_for(:active_scope, self), time, time)
  }
end

#with_identity_cacheObject



65
66
67
68
# File 'lib/state_of_the_nation.rb', line 65

def with_identity_cache
  add_child_methods(plural: @association_plural, single: false, with_identity_cache: true)
  self
end