gyroscope 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf459b04ef38e80fc048d485337c197519ff6950
4
+ data.tar.gz: b4628759f58aa82af0033d578281efa2f5651b16
5
+ SHA512:
6
+ metadata.gz: a987c3ccf09daae87b46cc9ca50ba28d1b79580f4c8a35c8df663632d084df2efaffcba6b978459e1a6ff7861272b2b91eb45c0f1d97a527ee4b0600e37aa661
7
+ data.tar.gz: 24c25a95db77984b69b01f900902a251f013e2c2fd2703fbaeab0fd99f81de32a3722d6077e26151af664d682ecfa908aa8f9e61fc04c045bae601227c74899d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gyroscope.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Donald Plummer
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,53 @@
1
+ # Gyroscope
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'gyroscope'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install gyroscope
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ module Search
24
+ class User < Gyroscope::SearchBase
25
+ attribute :name, String
26
+ end
27
+ end
28
+
29
+ searcher = Search::User.new(name: "Bob Dole")
30
+
31
+ searcher.search # => user records!
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
37
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
38
+ prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To
41
+ release a new version, update the version number in `version.rb`, and then run
42
+ `bundle exec rake release`, which will create a git tag for the version, push
43
+ git commits and tags, and push the `.gem` file to
44
+ [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/avvo/gyroscope.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
53
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gyroscope"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gyroscope.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gyroscope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gyroscope"
8
+ spec.version = Gyroscope::VERSION
9
+ spec.authors = ["Chris Wilhelm", "Donald Plummer"]
10
+ spec.email = ["cwilhelm@avvo.com", "dplummer@avvo.com"]
11
+
12
+ spec.summary = %q{Build ActiveRecord search scopes from params hash}
13
+ spec.description = %q{Coerces values in params hash for search scoping}
14
+ spec.homepage = "https://github.com/avvo/gyroscope"
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_dependency "activemodel"
23
+ spec.add_dependency "activerecord"
24
+ spec.add_dependency "activesupport"
25
+ spec.add_dependency "virtus"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.11"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ end
@@ -0,0 +1,16 @@
1
+ module Gyroscope
2
+ class IntegerList < Virtus::Attribute
3
+ strict true
4
+ required false
5
+
6
+ def coerce(values)
7
+ return nil if values.nil?
8
+
9
+ attr = Virtus::Attribute.build Integer, strict: true
10
+ Array(values).
11
+ map {|value| value.respond_to?(:split) ? value.split(',') : value }.
12
+ flatten.
13
+ map { |v| attr.coerce(v) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,87 @@
1
+ require "virtus"
2
+ require "active_model"
3
+
4
+ module Gyroscope
5
+ class SearchBase
6
+ include Virtus.model strict: true, required: false
7
+ include ActiveModel::Validations
8
+
9
+ attribute :page, Integer, default: 1
10
+ attribute :per_page, Integer, default: 30
11
+ attribute :order, String
12
+
13
+ validate :validate_order
14
+
15
+ def initialize(*args)
16
+ super(*args)
17
+ normalize_order attributes[:order]
18
+ end
19
+
20
+ def target_model
21
+ "::#{self.class.name.demodulize}".constantize
22
+ end
23
+
24
+ def search
25
+ # is there a more generic ActiveModel validation error?
26
+ unless valid?
27
+ Rails.logger.debug("the errors: #{errors.inspect}")
28
+ end
29
+ fail ActiveModel::ForbiddenAttributesError, "Check your default values for correctness" unless valid?
30
+
31
+ build_search_scope
32
+ end
33
+
34
+ def normalize_order(ordering)
35
+ return unless attributes[:order]
36
+
37
+ unless /\./.match ordering
38
+ self.order = "#{target_model.table_name}.#{ordering}"
39
+ else
40
+ self.order = ordering
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def base_scope
47
+ scope = target_model
48
+
49
+ if attributes[:page] || attributes[:per_page]
50
+ scope = pagination_scope(scope)
51
+ end
52
+ if attributes[:order]
53
+ scope = scope.order(attributes[:order])
54
+ end
55
+
56
+ scope
57
+ end
58
+
59
+ def pagination_scope(scope)
60
+ pagination_scope = {}
61
+ pagination_scope[:page] = attributes[:page] if attributes[:page]
62
+ pagination_scope[:per_page] = attributes[:per_page] if attributes[:per_page]
63
+
64
+ scope.paginate(pagination_scope)
65
+ end
66
+
67
+ def build_search_scope
68
+ base_scope
69
+ end
70
+
71
+ def validate_order
72
+ return unless attributes[:order]
73
+ split_order = attributes[:order].split(' ')
74
+
75
+ key, direction = split_order
76
+
77
+ errors.add(:order, 'Ordering on disallowed key') unless allowed_order_keys.include? key
78
+ errors.add(:order, 'Invalid sort direction') unless direction && %w(asc desc).include?(direction.downcase)
79
+ end
80
+
81
+ def allowed_order_keys
82
+ target_model.new.attributes.keys.map do |key|
83
+ "#{target_model.table_name}.#{key}"
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Gyroscope
2
+ VERSION = "0.1.0"
3
+ end
data/lib/gyroscope.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "gyroscope/version"
2
+
3
+ module Gyroscope
4
+ autoload :SearchBase, "gyroscope/search_base"
5
+ autoload :IntegerList, "gyroscope/integer_list"
6
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gyroscope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Wilhelm
8
+ - Donald Plummer
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: activerecord
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: activesupport
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: virtus
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '1.11'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '1.11'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rake
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '10.0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '10.0'
98
+ - !ruby/object:Gem::Dependency
99
+ name: rspec
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '3.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '3.0'
112
+ description: Coerces values in params hash for search scoping
113
+ email:
114
+ - cwilhelm@avvo.com
115
+ - dplummer@avvo.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - gyroscope.gemspec
130
+ - lib/gyroscope.rb
131
+ - lib/gyroscope/integer_list.rb
132
+ - lib/gyroscope/search_base.rb
133
+ - lib/gyroscope/version.rb
134
+ homepage: https://github.com/avvo/gyroscope
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.5.1
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Build ActiveRecord search scopes from params hash
158
+ test_files: []