Class: LetterGroup::Presenter
- Inherits:
-
Object
- Object
- LetterGroup::Presenter
- Defined in:
- lib/letter_group/presenter.rb
Constant Summary collapse
- LETTER_MATCHER =
true makes it case insensitive
-> to_match, letter { to_match =~ Regexp.new("^#{letter}", true) }
- ALPHABET =
("a".."z").to_a.freeze
- ALPHABET_AND_OTHER =
(ALPHABET + ["other"]).freeze
- DEFAULT_SELECTED =
("a".."c").to_a.freeze
- IN_GROUPS =
ALPHABET_AND_OTHER.each_slice(3)
Instance Attribute Summary collapse
-
#array_of_hashes ⇒ Object
readonly
Returns the value of attribute array_of_hashes.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#selected ⇒ Object
readonly
Returns the value of attribute selected.
-
#total_selected ⇒ Object
readonly
Returns the value of attribute total_selected.
Instance Method Summary collapse
- #count_for(letters) ⇒ Object
- #each ⇒ Object
-
#initialize(array_of_hashes = [], alpha_key:, unique_key:, selected: nil, field_groups: {}) ⇒ Presenter
constructor
A new instance of Presenter.
Constructor Details
#initialize(array_of_hashes = [], alpha_key:, unique_key:, selected: nil, field_groups: {}) ⇒ Presenter
Returns a new instance of Presenter.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/letter_group/presenter.rb', line 17 def initialize(array_of_hashes = [], alpha_key:, unique_key:, selected: nil, field_groups: {}) # With this sort of data triage many of the result rows must be combined to represent a single data issue. # (the nature of SQL JOINS) # Thus we can only get a count by tallying up the total for each group. # since they each will know how to find their own unique problems @array_of_hashes = array_of_hashes # responds to each with PGresult-like tuples @fields = array_of_hashes.first ? array_of_hashes.first.keys : [] @alpha_key = alpha_key.to_s # like "leads_last_name" or "leads_email" @unique_key = unique_key.to_s # like "leads_id" or "leads_email" raise ArgumentError, "#{self.class} Looks like alpha_key (#{@alpha_key} was not part of the select on fields: #{@fields.inspect}" unless @fields.empty? || @fields.include?(@alpha_key) raise ArgumentError, "#{self.class} Looks like unique_key (#{@unique_key} was not part of the select on fields: #{@fields.inspect}" unless @fields.empty? || @fields.include?(@unique_key) @field_groups = field_groups || {} @groups = {} divide_into_letters set_selected(selected) @total_selected = each.inject(0) {|memo, group| memo += group.total; memo } raise ArgumentError, "Unable to allocate all data into groups" unless @array_of_hashes.empty? end |
Instance Attribute Details
#array_of_hashes ⇒ Object (readonly)
Returns the value of attribute array_of_hashes.
15 16 17 |
# File 'lib/letter_group/presenter.rb', line 15 def array_of_hashes @array_of_hashes end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
15 16 17 |
# File 'lib/letter_group/presenter.rb', line 15 def groups @groups end |
#selected ⇒ Object (readonly)
Returns the value of attribute selected.
15 16 17 |
# File 'lib/letter_group/presenter.rb', line 15 def selected @selected end |
#total_selected ⇒ Object (readonly)
Returns the value of attribute total_selected.
15 16 17 |
# File 'lib/letter_group/presenter.rb', line 15 def total_selected @total_selected end |
Instance Method Details
#count_for(letters) ⇒ Object
45 46 47 |
# File 'lib/letter_group/presenter.rb', line 45 def count_for(letters) groups.values_at(*letters).flatten.inject(0) {|memo, group| memo += group.total; memo } end |
#each ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/letter_group/presenter.rb', line 36 def each return enum_for(:each) unless block_given? # Sparkling magic! selected.each do |letter| for_letter = groups[letter] next unless for_letter yield for_letter end end |