Module: Parliament::Utils::Helpers::HistoryHelper

Includes:
NTriple::Utils
Defined in:
lib/parliament/utils/helpers/history_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#year_separator=(value) ⇒ Object (writeonly)

Sets the attribute year_separator

Parameters:

  • value

    the value to set the attribute year_separator to.



8
9
10
# File 'lib/parliament/utils/helpers/history_helper.rb', line 8

def year_separator=(value)
  @year_separator = value
end

Class Method Details

.add(data) ⇒ Hash

Adds data to history object, separating current Grom::Nodes from historic Grom::Nodes and setting start date of earliest Grom::Node

Parameters:

  • data (Array)

    Array of Grom::Nodes

Returns:

  • (Hash)

    History object with Grom::Nodes divided into historic and current



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/parliament/utils/helpers/history_helper.rb', line 42

def self.add(data)
  return if data.nil? || data.empty?
  # Set history if it has not already been set
  history
  data.each do |entry|
    # Current
    @history[:current] << entry if entry.try(:end_date).nil?

    # Exit if there is no end date or if the end date is not a date
    next if entry.try(:end_date).try(:year).try(:to_f).nil?

    # e.g. If current year is 2017, end date is 2008 and year_separator is 10...
    # year = ((2017-2008)/10).ceil) * 10 = 0
    # But as it happened within the last 10 years, we want it to be placed in the 10 year time period, not 0
    year = ((Time.now.year.to_f - entry.end_date.year.to_f) / year_separator).ceil * year_separator
    year = year_separator if year == 0

    @history[:years][year] = [] unless @history[:years][year]
    @history[:years][year] << entry

    if entry.respond_to?(:start_date)
      @history[:start] = entry.start_date if @history[:start].nil? || @history[:start] > entry.start_date
    end
  end

  # Sort current Grom::Nodes by start date
  @history[:current].sort_by!(&:start_date).reverse!
  sort_history
end

.historyHash

Creates/sets an empty history object

Returns:

  • (Hash)

    Object with properties of start, current and years



20
21
22
23
24
25
26
27
28
# File 'lib/parliament/utils/helpers/history_helper.rb', line 20

def self.history
  @history ||= {
    start:   nil,
    current: [],
    # Keys are the time periods
    # Value is array of Grom::Nodes that have occured during this time period
    years:   {}
  }
end

.resetnil

Resets history

Returns:

  • (nil)

    Resets history to nil



33
34
35
# File 'lib/parliament/utils/helpers/history_helper.rb', line 33

def self.reset
  @history = nil
end

.sort_historyHash

Sorts Grom::Nodes first by end date, then by start date (if same end date)

Returns:

  • (Hash)

    History object with divided and sorted Grom::Nodes



75
76
77
78
79
80
81
82
83
84
# File 'lib/parliament/utils/helpers/history_helper.rb', line 75

def self.sort_history
  @history[:years].keys.each do |year|
    @history[:years][year] = Parliament::NTriple::Utils.multi_direction_sort(
      {
        list:       @history[:years][year],
        parameters: { end_date: :desc, start_date: :desc }
      }
    )
  end
end

.year_separatorInteger

Returns year_separator variable (getter method)

Returns:

  • (Integer)

    Number of years used to divide up the timeline into time periods



13
14
15
# File 'lib/parliament/utils/helpers/history_helper.rb', line 13

def self.year_separator
  @year_separator ||= 10
end