plain_query 0.0.1

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
+ SHA256:
3
+ metadata.gz: 61ae47934ae841a4229172526bf21e44e8f038a297596624102035277b7daf0a
4
+ data.tar.gz: 0f70b4d457dddb4a01f971afa7e229589178c9200ed785bf97258bbbf8f179d4
5
+ SHA512:
6
+ metadata.gz: c1c425097bdd73f3d10fe3c29ae62cbb6410073fb3263ed9be355fa16459f916ec0b41c50a3bd9e8dbedf9da23c19d85ebd056795e141268b2925d40ab6a5fad
7
+ data.tar.gz: df14cce0826761f2c136ace6409ade132925b04a641d890359534d7eb89585188e25dd9f3eb97110e496755ee79baf6d7f39ed77bcc4f8012217e8e8da63b11d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at glee812@yandex.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in plain_query.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Gleb
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,146 @@
1
+ # PlainQuery
2
+
3
+ PlainQuery is a simple gem that helps you write clear and flexible query objects.
4
+ Main task for this gem is performing complex querying on active record relation, make it intuitive.
5
+
6
+ It helps in decomposing your fat ActiveRecord models and keeping your code slim and readable by extracting complex SQL queries or scopes into the separated classes that are easy to write, read and use.
7
+
8
+ PlainQuery is useful when you need to build one or more queries based on incoming parameters in the request. It hides scope building logic inside query class and allows you to structure the building of the scope.
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'plain_query'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install plain_query
24
+
25
+ ## Usage
26
+ ### Setting up a query object
27
+ For setting up a query object you need to inherit your query class from PlainQuery.
28
+ Then you need to describe query steps by using query_step method.
29
+ Query steps perform in writing order.
30
+
31
+ ```rb
32
+ class UsersQuery < PlainQuery
33
+ model User
34
+
35
+ query_step :filter_by_activity, if: { options[:only_active] }
36
+ query_step :filter_by_phone_presence
37
+ query_step :order_by_name
38
+
39
+ def filter_by_activity
40
+ relation.where(active: true)
41
+ end
42
+
43
+ def filter_by_phone_presence
44
+ relation.where.not(phone: nil)
45
+ end
46
+
47
+ def order_by_name
48
+ relation.order(name: :asc)
49
+ end
50
+ end
51
+ ```
52
+
53
+ ### Query calling
54
+
55
+ ```rb
56
+ users = UsersQuery.call(User.all, only_active: true)
57
+ ```
58
+
59
+ Query object implements `#call` method with two arguments:
60
+
61
+ `relation` - Base scope which will be mutated inside query object. (`User.all` in example).
62
+ If you dont pass it - will be used default scope from model declaration inside query object.
63
+
64
+ `options` - Any data which will be used inside query steps or execution conditions. (`only_active: true` in example).
65
+
66
+ Query object returns scope builded by query steps execution.
67
+
68
+ ### query_step
69
+ `query_step` is a main part of query building.
70
+
71
+ It declares which query change method will be executed, condition of execution and order of query change methods execution.
72
+
73
+ It has several arguments:
74
+
75
+ ```rb
76
+ query_step STEP_NAME, CONDITION_OF_EXECUTION
77
+ ```
78
+
79
+ `STEP_NAME` is a name of method which will be executed.
80
+
81
+ `CONDITION_OF_EXECUTION` is a method which allows or denieds execution of query step.
82
+ Type of condition can be `if:` or `unless:`. Condition definition can be proc (lambda) or some query object method name.
83
+
84
+ ### Using in Active Record model scope.
85
+ First of all you need to set correct model name inside query object.
86
+
87
+ It uses for correct base scope building without passing relation to query object.
88
+
89
+ ```rb
90
+ model User
91
+ ```
92
+
93
+ ```rb
94
+ class User < ActiveRecord::Base
95
+ scope :active_clients, ActiveClientsQuery
96
+ end
97
+ ```
98
+
99
+ ```rb
100
+ class ActiveClientsQuery < PlainQuery
101
+ model User
102
+
103
+ query_step :filter_by_activity
104
+ query_step :filter_by_role
105
+
106
+ def filter_by_activity
107
+ relation.where(active: true)
108
+ end
109
+
110
+ def filter_by_role
111
+ relation.where(role: :client)
112
+ end
113
+ end
114
+ ```
115
+
116
+ And then you can use scope from model:
117
+
118
+ ```rb
119
+ User.active_clients
120
+ ```
121
+
122
+ Also you can pass to Active Record scope some options:
123
+
124
+ ```rb
125
+ class User < ActiveRecord::Base
126
+ scope :active_clients, -> { ActiveClientsQuery.call(self, option: true) }
127
+ end
128
+ ```
129
+
130
+ ## Development
131
+
132
+ 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.
133
+
134
+ 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).
135
+
136
+ ## Contributing
137
+
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/plain_query. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
139
+
140
+ ## License
141
+
142
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
143
+
144
+ ## Code of Conduct
145
+
146
+ Everyone interacting in the PlainQuery project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/plain_query/blob/master/CODE_OF_CONDUCT.md).
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 "plain_query"
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__)
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
@@ -0,0 +1,75 @@
1
+ # Realises methods for scope building by execution of sequence of steps
2
+ # Query class defines query steps:
3
+ # query_step :filter_by_status, if: -> { options[:status].present? }
4
+ # def filter_by_status
5
+ # relation.where(status: options[:status])
6
+ # end
7
+ class PlainQuery
8
+ attr_reader :relation, :options
9
+
10
+ def initialize(relation, options)
11
+ @relation = relation
12
+ @options = options
13
+ @steps = self.class.steps
14
+
15
+ # Validates initial relation format. Allowed only ActiveRecord::Relation, model class.
16
+ if relation.nil?
17
+ raise(RelationRequired, 'Queries require a base relation defined')
18
+ elsif !relation.is_a?(ActiveRecord::Relation)
19
+ raise(RelationRequired, 'Queries accept only ActiveRecord::Relation as input')
20
+ end
21
+ end
22
+
23
+ def exec_query
24
+ @steps.each do |step_name, params|
25
+ # Handles if condition
26
+ if (params[:if] && !exec_condition(params[:if]))
27
+ next
28
+ end
29
+
30
+ # Handles unless condition
31
+ if (params[:unless] && exec_condition(params[:unless]))
32
+ next
33
+ end
34
+
35
+ # Executes query mutation and checks that step returns ActiveRecord::Relation
36
+ mutated_query = send(step_name)
37
+ if !mutated_query.is_a?(ActiveRecord::Relation)
38
+ raise(RelationIsIncorrect, 'Scope must be ActiveRecord::Relation')
39
+ end
40
+
41
+ @relation = mutated_query
42
+ end
43
+
44
+ @relation
45
+ end
46
+
47
+ # Executes if and unless conditions, conditions must contain object method name or proc (lambda)
48
+ def exec_condition(condition)
49
+ if condition.is_a?(String) || condition.is_a?(Symbol)
50
+ !!send(condition)
51
+ elsif condition.is_a?(Proc)
52
+ !!instance_exec(&condition)
53
+ else
54
+ raise(ConditionFormatIsIncorrect, 'Condition must be method name or proc')
55
+ end
56
+ end
57
+
58
+ class << self
59
+ def call(relation = @model&.all, options = {})
60
+ new(relation, options).exec_query
61
+ end
62
+
63
+ def query_step(step_name, params = {})
64
+ steps << [step_name, params]
65
+ end
66
+
67
+ def steps
68
+ @steps ||= []
69
+ end
70
+
71
+ def model(model)
72
+ @model = model
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ class PlainQuery
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "plain_query/version"
2
+ require "plain_query/logic"
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "plain_query/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "plain_query"
8
+ spec.version = PlainQuery::VERSION
9
+ spec.authors = ["gl-pv"]
10
+ spec.email = ["gleeb812812@yandex.ru"]
11
+
12
+ spec.summary = %q{Builds query object by declaration sequence of query building steps}
13
+ spec.description = %q{PlainQuery is a simple gem that helps you write clear and flexible query objects}
14
+ spec.homepage = "https://github.com/gl-pv/plain_query"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/gl-pv/plain_query"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.17"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plain_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - gl-pv
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-10 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: PlainQuery is a simple gem that helps you write clear and flexible query
56
+ objects
57
+ email:
58
+ - gleeb812812@yandex.ru
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/plain_query.rb
74
+ - lib/plain_query/logic.rb
75
+ - lib/plain_query/version.rb
76
+ - plain_query.gemspec
77
+ homepage: https://github.com/gl-pv/plain_query
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/gl-pv/plain_query
82
+ source_code_uri: https://github.com/gl-pv/plain_query
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.9
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Builds query object by declaration sequence of query building steps
102
+ test_files: []