Class: Pollster::PollsterChartPollQuestions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pollster/models/pollster_chart_poll_questions.rb

Constant Summary collapse

HeaderLineRegex =
/\A(?<labels>[^\t]+(?:\t[^\t]+)*)\tpoll_slug\tsurvey_house\tstart_date\tend_date\tquestion_text\tsample_subpopulation\tobservations\tmargin_of_error\tmode\tpartisanship\tpartisan_affiliation\b/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ PollsterChartPollQuestions

Returns a new instance of PollsterChartPollQuestions.



73
74
75
# File 'lib/pollster/models/pollster_chart_poll_questions.rb', line 73

def initialize(array)
  @array = array
end

Class Method Details

.from_tsv(tsv) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/pollster/models/pollster_chart_poll_questions.rb', line 81

def self.from_tsv(tsv)
  lines = tsv.split(/(?:\r?\n)+/).reject(&:empty?)
  if m = HeaderLineRegex.match(lines[0])
    labels = m[:labels].split(/\t/)
    rows = lines[1..-1].map { |tsv_line| PollsterChartPollQuestion.from_tsv_line_with_labels(tsv_line, labels) }
    PollsterChartPollQuestions.new(rows)
  else
    raise ArgumentError.new("First line of TSV is `#{lines[0]}`, which does not match Pollster::PollsterChartPollQuestions::HeaderLineRegex")
  end
end

Instance Method Details

#each(&block) ⇒ Object



77
78
79
# File 'lib/pollster/models/pollster_chart_poll_questions.rb', line 77

def each(&block)
  @array.each(&block)
end