letter_group 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d65d470de1c64118554a38b944a92dfc20d3a517
4
+ data.tar.gz: 62de52f0c116d0d6cf8a098b1d1e6248c99caeb4
5
+ SHA512:
6
+ metadata.gz: 8ccb5bbadd1690fa0e19ed5385388c722fd97189ef8896aab700258a34c34d223121e667afe93d411c399ee2ff8d2b46b17f69ca271a680bce503f4699879fa8
7
+ data.tar.gz: cebc8accee5d9170987dcd161edca63cd79007ba0a693c1843343b657544b633ef35e60baa08249ca92d8800454398c13c6acbcf7ead7253305992c51786d99f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in letter_group.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Trumaker, Inc & Peter Boling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # LetterGroup
2
+
3
+ Organize data results from raw sql queries (as with PGresult, or Dossier) intelligently.
4
+
5
+ See specs for examples.
6
+
7
+ No view code yet, for now just the logic.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'letter_group'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install letter_group
24
+
25
+ ## Usage
26
+
27
+ See specs for usage examples.
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`.
34
+
35
+ ## Maintenance
36
+
37
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/letter_group/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "letter_group"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'letter_group/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "letter_group"
8
+ spec.version = LetterGroup::VERSION
9
+ spec.authors = ["Peter Boling"]
10
+ spec.email = ["peter.boling@gmail.com"]
11
+
12
+ spec.summary = %q{Organize data results from raw sql queries intelligently.}
13
+ spec.description = %q{Organize data results from raw sql queries (as with PGresult, or Dossier) intelligently.}
14
+ spec.homepage = "https://github.com/trumaker/letter_group"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ spec.add_development_dependency "pry", "~> 0.10"
26
+ end
@@ -0,0 +1,6 @@
1
+ require "letter_group/version"
2
+ require "letter_group/group"
3
+ require "letter_group/presenter"
4
+
5
+ module LetterGroup
6
+ end
@@ -0,0 +1,93 @@
1
+ # Whereas multiple rows in a result set from a join with a group by may be more correctly considered as a single result,
2
+ # this class represents a group of rows together representing something unique
3
+ # It also allows for grouping the data in each hash into rows within each row, using the concept of field_groups.
4
+ # Note: the group total is often 1 if there are no other rows to group with it
5
+ #
6
+ # Example: If we have the following rows (hashes where column names are keys):
7
+ # params array_of_hashes - a set of data that has not yet been organized into distinct results
8
+ # e.g.
9
+ # [
10
+ # {"leads_id"=>"31998","ol_id"=>"27765","leads_first_name"=>"Dan","ol_first_name"=>"Taylor"},
11
+ # {"leads_id"=>"32072","ol_id"=>"27846","leads_first_name"=>"Dave","ol_first_name"=>"Yancy"}, # <= since leads_id is the unique_key
12
+ # {"leads_id"=>"32072","ol_id"=>"28428","leads_first_name"=>"Davis","ol_first_name"=>"Jaunty"} # <= these two are really a single result
13
+ # ]
14
+ # param letter - the letter of the alphabet which this set of results are being grouped together by
15
+ # used by the presentation as the heading for the group.
16
+ # e.g. "D"
17
+ # default ""
18
+ # param unique_key - The key which designates uniqueness.
19
+ # e.g. "leads_id"
20
+ # default nil
21
+ # param fields - optional, and if field_groups has keys, then passed in value of fields is ignored.
22
+ # e.g. ["leads_id","ol_id","leads_first_name","ol_first_name","leads_last_name","ol_last_name"]
23
+ # default []
24
+ # param field_groups - optional, and if provided will be used to determine fields.
25
+ # If field_groups and fields are both provided field_groups takes precedence
26
+ # Used when you have multiple columns which are similar in nature and the data would be nicely stacked in the view.
27
+ # e.g. {
28
+ # "id" => ["leads_id", "ol_id"],
29
+ # "name" => ["leads_first_name", "ol_first_name"]
30
+ # }
31
+ # default {}
32
+
33
+ module LetterGroup
34
+ class Group
35
+
36
+ attr_reader :rows, :letter, :total, :unique_key, :fields, :field_groups, :labels
37
+
38
+ def initialize(array_of_hashes = [], letter: "", unique_key: nil, fields: [], field_groups: {})
39
+ @array_of_hashes = array_of_hashes || []
40
+ @letter = (letter || "").upcase
41
+ # when unique_key is nil results in a single Tuple with a set for all the rows in this group.
42
+ @unique_key = unique_key
43
+ determine_fields(fields, field_groups)
44
+ fill_rows
45
+ end
46
+
47
+ def each
48
+ return enum_for(:each) unless block_given? # Sparkling magic!
49
+ rows.each do |_, tuple|
50
+ yield tuple
51
+ end
52
+ end
53
+
54
+ private
55
+ def unique_tuple_hash_builder
56
+ Hash.new do |hash, key|
57
+ hash[key] = @array_of_hashes.select {|x| x[@unique_key] == key }
58
+ end
59
+ end
60
+
61
+ def determine_fields(fields, field_groups)
62
+ field_groups ||= {}
63
+ if field_groups.keys.any?
64
+ @field_groups = field_groups
65
+ @fields = field_groups.keys
66
+ else
67
+ fields ||= []
68
+ @fields, non_matching_fields = fields.partition {|x| x == unique_key}
69
+ @fields.concat(non_matching_fields)
70
+ @field_groups = @fields.inject({}) {|memo, elem| memo[elem] = Array(elem); memo}
71
+ end
72
+ @num_fields = @fields.length
73
+ # The column names in the first field_group will be used as labels in the view.
74
+ # so for { "ID" => ["lead_id", "user_id"] }
75
+ # the labels are the array ["lead_id","user_id"]
76
+ @labels = if @field_groups.first
77
+ @field_groups.first[1]
78
+ else
79
+ []
80
+ end
81
+ end
82
+
83
+ def fill_rows
84
+ @unique_values = @array_of_hashes.map {|x| x[@unique_key] }.uniq
85
+ @rows = @unique_values.inject(unique_tuple_hash_builder) do |memo, value|
86
+ memo[value]
87
+ memo
88
+ end
89
+ @total = rows.length
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,100 @@
1
+ # Purpose:
2
+ # To assist with rendering any Array of Hashes like you get back from a raw sql query, Invariants, or Dossier Reports.
3
+ # Due to the generic nature, with very little configuration results can be organized
4
+ # and rendered with a single template which handles all current needs.
5
+
6
+ module LetterGroup
7
+ class Presenter
8
+
9
+ LETTER_MATCHER = -> to_match, letter { to_match =~ Regexp.new("^#{letter}", true) } # true makes it case insensitive
10
+ ALPHABET = ("a".."z").to_a.freeze
11
+ ALPHABET_AND_OTHER = (ALPHABET + ["other"]).freeze
12
+ DEFAULT_SELECTED = ("a".."c").to_a.freeze
13
+ IN_GROUPS = ALPHABET_AND_OTHER.each_slice(3)
14
+
15
+ attr_reader :array_of_hashes, :groups, :total_selected, :selected
16
+
17
+ def initialize(array_of_hashes = [], alpha_key:, unique_key:, selected: nil, field_groups: {})
18
+ # With this sort of data triage many of the result rows must be combined to represent a single data issue.
19
+ # (the nature of SQL JOINS)
20
+ # Thus we can only get a count by tallying up the total for each group.
21
+ # since they each will know how to find their own unique problems
22
+ @array_of_hashes = array_of_hashes # responds to each with PGresult-like tuples
23
+ @fields = array_of_hashes.first ? array_of_hashes.first.keys : []
24
+ @alpha_key = alpha_key.to_s # like "leads_last_name" or "leads_email"
25
+ @unique_key = unique_key.to_s # like "leads_id" or "leads_email"
26
+ 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)
27
+ 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)
28
+ @field_groups = field_groups || {}
29
+ @groups = {}
30
+ divide_into_letters
31
+ set_selected(selected)
32
+ @total_selected = each.inject(0) {|memo, group| memo += group.total; memo }
33
+ raise ArgumentError, "Unable to allocate all data into groups" unless @array_of_hashes.empty?
34
+ end
35
+
36
+ def each
37
+ return enum_for(:each) unless block_given? # Sparkling magic!
38
+ selected.each do |letter|
39
+ for_letter = groups[letter]
40
+ next unless for_letter
41
+ yield for_letter
42
+ end
43
+ end
44
+
45
+ def count_for(letters)
46
+ groups.values_at(*letters).flatten.inject(0) {|memo, group| memo += group.total; memo }
47
+ end
48
+
49
+ private
50
+
51
+ def enhance_by_unique_key(rows)
52
+ unique_keys = rows.map {|x| x[@unique_key]}.uniq
53
+ more_rows_with_same_unique_keys, @array_of_hashes = @array_of_hashes.partition do |row|
54
+ unique_keys.include?(row[@unique_key])
55
+ end
56
+ more_rows_with_same_unique_keys
57
+ end
58
+
59
+ def divide_into_letters
60
+ ALPHABET.each do |letter|
61
+ rows, @array_of_hashes = @array_of_hashes.partition do |hash|
62
+ LETTER_MATCHER.call(hash[@alpha_key], letter)
63
+ end
64
+ rows.concat(enhance_by_unique_key(rows))
65
+ groups[letter] = Group.new(
66
+ rows,
67
+ letter: letter,
68
+ unique_key: @unique_key,
69
+ fields: @fields,
70
+ field_groups: @field_groups
71
+ )
72
+ end
73
+ rows, @array_of_hashes = @array_of_hashes.partition do |_|
74
+ # Whatever is left over goes in "other" (may not have an alpha first character)
75
+ # And @array_of_hashes becomes empty.
76
+ true
77
+ end
78
+ groups["other"] = Group.new(
79
+ rows,
80
+ letter: "other",
81
+ unique_key: @unique_key,
82
+ fields: @fields,
83
+ field_groups: @field_groups
84
+ )
85
+ end
86
+
87
+ def set_selected(selected)
88
+ @selected = if !selected.nil? && !selected.empty?
89
+ custom_selected = selected.split(",").map {|x| x.strip}
90
+ ALPHABET_AND_OTHER.select {|x| custom_selected.include?(x) }
91
+ else
92
+ DEFAULT_SELECTED
93
+ end
94
+ end
95
+
96
+ def self.group_for_letter(letter)
97
+ IN_GROUPS.detect {|group| group.include?(letter)}.join(",")
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module LetterGroup
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letter_group
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Boling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description: Organize data results from raw sql queries (as with PGresult, or Dossier)
70
+ intelligently.
71
+ email:
72
+ - peter.boling@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - letter_group.gemspec
88
+ - lib/letter_group.rb
89
+ - lib/letter_group/group.rb
90
+ - lib/letter_group/presenter.rb
91
+ - lib/letter_group/version.rb
92
+ homepage: https://github.com/trumaker/letter_group
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Organize data results from raw sql queries intelligently.
116
+ test_files: []