advanced 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03d013abf9f84626c703a7776d3604ae6214fa87
4
+ data.tar.gz: c1d2e25e5d09e10aabce2f4740f8fb8b77f091cf
5
+ SHA512:
6
+ metadata.gz: eea046ecbc5d2be4827b866d2b0170bbcd9a5c2ba0071184489f85d7ede4ea5ef68b5c7f158cd9505e87c5d81220dd6d150f026d253612b5d0e74645a6da8f8c
7
+ data.tar.gz: d8ad6abde094aadff5ca891dc319e7635bfb0694c4fd7103ed469bc21d6bed3a06b81214cf14745310d15a6e2e5996874bd50a5ef4af3519ad9468f78c22e926
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in advanced.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'activemodel'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ray Zane
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.
@@ -0,0 +1,41 @@
1
+ # Advanced
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/advanced`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'advanced'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install advanced
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Ray Zane/advanced.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -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
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'advanced/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "advanced"
8
+ spec.version = Advanced::VERSION
9
+ spec.authors = ["Ray Zane"]
10
+ spec.email = ["ray@promptworks.com"]
11
+
12
+ spec.summary = %q{Search with objects!}
13
+ spec.homepage = "https://github.com/rzane/advanced"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'activesupport'
24
+ spec.add_dependency 'pipey'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "advanced"
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(__FILE__)
@@ -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
@@ -0,0 +1,7 @@
1
+ require 'advanced/version'
2
+ require 'advanced/search'
3
+ require 'advanced/search_form'
4
+ require 'advanced/active_record'
5
+
6
+ module Advanced
7
+ end
@@ -0,0 +1,30 @@
1
+ module Advanced
2
+ module ActiveRecord
3
+ module DSL
4
+ def where_eq(param, column: param)
5
+ define_search param, [param] do |value|
6
+ where column => value
7
+ end
8
+ end
9
+
10
+ def where_any_eq(param, column: param)
11
+ define_search param, [param] do |values|
12
+ values = values.reject(&:blank?)
13
+ where column => values if values.any?
14
+ end
15
+ end
16
+
17
+ def where_lt(param, column: param)
18
+ define_search param, [param] do |value|
19
+ where(arel_table[column].lt(value))
20
+ end
21
+ end
22
+
23
+ def where_gt(param, column: param)
24
+ define_search param, [param] do |value|
25
+ where(arel_table[column].gt(value))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ module Advanced
2
+ module Builders
3
+ class Scope < Module
4
+ def initialize(search, name)
5
+ define_method name do |params = {}|
6
+ search.new(all).call(params)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ module Advanced
2
+ class Definition
3
+ KEY_TYPES = %i[key keyreq]
4
+
5
+ def initialize
6
+ @names = []
7
+ end
8
+
9
+ def add(names = [])
10
+ @names += names
11
+ end
12
+
13
+ def parameter_names_for(klass)
14
+ values = klass.steps.flat_map do |meth|
15
+ klass.instance_method(meth).parameters.reduce([]) do |acc, (type, name)|
16
+ KEY_TYPES.include?(type) ? (acc + [name]) : acc
17
+ end
18
+ end
19
+
20
+ values + @names
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ require 'pipey'
2
+ require 'advanced/definition'
3
+ require 'advanced/builders'
4
+
5
+ module Advanced
6
+ class Search < Pipey::Chain
7
+ extend Pipey::Steps::Scanner[/\A(search|then)_/]
8
+ extend Pipey::Extensions::RequiredKeys
9
+ extend Pipey::Extensions::IgnoreNil
10
+
11
+ class << self
12
+ def definition
13
+ @definition ||= Definition.new
14
+ end
15
+
16
+ def parameter_names
17
+ definition.parameter_names_for(self)
18
+ end
19
+
20
+ def use(other)
21
+ define_method "search_#{other.name}" do |**opts|
22
+ other.call(scope, **opts)
23
+ end
24
+ end
25
+
26
+ def scope(name = :search)
27
+ Builders::Scope.new(self, name)
28
+ end
29
+
30
+ def define_search(name, requirements = [], permits = [], &block)
31
+ definition.add(requirements)
32
+ definition.add(permits)
33
+
34
+ define_method "search_#{name}" do |**opts|
35
+ if requirements.all? { |k| opts[k] }
36
+ instance_exec(
37
+ *opts.values_at(*requirements),
38
+ *opts.values_at(*permits),
39
+ &block
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ alias scope __getobj__
47
+
48
+ def call(params = {})
49
+ super(params.to_h)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,69 @@
1
+ require 'set'
2
+ require 'active_support/core_ext/object/blank'
3
+
4
+ module Advanced
5
+ class SearchForm
6
+ begin
7
+ require 'active_model'
8
+ include ActiveModel::Model
9
+ rescue LoadError
10
+ end
11
+
12
+ def self.attribute_names
13
+ @attribute_names ||= Set.new
14
+ end
15
+
16
+ def self.attribute(*names)
17
+ names.flatten.each do |name|
18
+ attribute_names << name
19
+ attr_accessor name
20
+ end
21
+ end
22
+
23
+ def self.search(value)
24
+ attribute(*value.parameter_names)
25
+ end
26
+
27
+ def self.nested(key, form)
28
+ ivar = :"@#{key}"
29
+
30
+ attribute_names << key
31
+
32
+ define_method key do
33
+ instance_variable_get(ivar) ||
34
+ instance_variable_set(ivar, form.new)
35
+ end
36
+
37
+ define_method "#{key}=" do |values|
38
+ instance_variable_set(ivar, form.new(values))
39
+ end
40
+ end
41
+
42
+ # We know exactly what parameters are whitelisted,
43
+ # so, we can skip by AC::Parameters.
44
+ def initialize(opts = nil)
45
+ if opts.respond_to? :to_unsafe_h
46
+ super opts.to_unsafe_h
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ def blank?
53
+ to_h.blank?
54
+ end
55
+
56
+ # Pull out the blank values. Recursively check if the
57
+ # resulting object responds to #to_search_h, as this
58
+ # would indicate a SearchForm object.
59
+ def to_search_h
60
+ self.class.attribute_names.reduce({}) do |acc, key|
61
+ value = public_send(key)
62
+ value = value.to_search_h if value.respond_to?(:to_search_h)
63
+ value = value.presence
64
+ value ? acc.merge(key => value) : acc
65
+ end
66
+ end
67
+ alias to_h to_search_h
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Advanced
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: advanced
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ray Zane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pipey
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - ray@promptworks.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - advanced.gemspec
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/advanced.rb
101
+ - lib/advanced/active_record.rb
102
+ - lib/advanced/builders.rb
103
+ - lib/advanced/definition.rb
104
+ - lib/advanced/search.rb
105
+ - lib/advanced/search_form.rb
106
+ - lib/advanced/version.rb
107
+ homepage: https://github.com/rzane/advanced
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.10
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Search with objects!
131
+ test_files: []