plain_model 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 +7 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +35 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/plain_model/association_builder.rb +22 -0
- data/lib/plain_model/errors.rb +8 -0
- data/lib/plain_model/merge_includes.rb +53 -0
- data/lib/plain_model/model.rb +14 -0
- data/lib/plain_model/modeling/associatable.rb +64 -0
- data/lib/plain_model/modeling/base.rb +26 -0
- data/lib/plain_model/modeling/queryable.rb +21 -0
- data/lib/plain_model/query_builder.rb +17 -0
- data/lib/plain_model/querying/base.rb +61 -0
- data/lib/plain_model/querying/except.rb +24 -0
- data/lib/plain_model/querying/includes.rb +32 -0
- data/lib/plain_model/querying/where.rb +29 -0
- data/lib/plain_model/querying/with_model.rb +30 -0
- data/lib/plain_model/version.rb +5 -0
- data/lib/plain_model.rb +10 -0
- data/plain_model.gemspec +33 -0
- metadata +140 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dbfb75e773e360168629f706d2180999618cb6bd9e1021be975beafa22e5106c
|
|
4
|
+
data.tar.gz: fd281798e57e733940630ed0c930ed83a8b6d66562f04d7aaf541d2100a8b095
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a65be93c437fd1e95933448aac1b74ea859c2db7e573ce4ff56689ea8c603d6f8b34d93a03673183ccdb63c2055b6661cd2e0742b946b66d53a56b3c6fc8b8b2
|
|
7
|
+
data.tar.gz: 126876f5467e55745540232a77e566cf99366a13be74c015298da16cb48b07503920f80869e1ed45775eef5f45c8553c250ca6440c94e83f0abe8a21664905ae
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.5
|
|
3
|
+
|
|
4
|
+
Metrics/LineLength:
|
|
5
|
+
Max: 120
|
|
6
|
+
|
|
7
|
+
Metrics/BlockLength:
|
|
8
|
+
Enabled: false
|
|
9
|
+
|
|
10
|
+
Metrics/MethodLength:
|
|
11
|
+
Enabled: false
|
|
12
|
+
|
|
13
|
+
Metrics/AbcSize:
|
|
14
|
+
Enabled: false
|
|
15
|
+
|
|
16
|
+
Metrics/CyclomaticComplexity:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
19
|
+
Style/Documentation:
|
|
20
|
+
Enabled: false
|
|
21
|
+
|
|
22
|
+
Style/SymbolArray:
|
|
23
|
+
EnforcedStyle: brackets
|
|
24
|
+
|
|
25
|
+
Layout/IndentFirstArrayElement:
|
|
26
|
+
IndentationWidth: 4
|
|
27
|
+
|
|
28
|
+
Layout/IndentFirstHashElement:
|
|
29
|
+
IndentationWidth: 4
|
|
30
|
+
|
|
31
|
+
Layout/IndentFirstArgument:
|
|
32
|
+
IndentationWidth: 4
|
|
33
|
+
|
|
34
|
+
Layout/ClosingParenthesisIndentation:
|
|
35
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 senid231@gmail.com. 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
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Denis Talakevich
|
|
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,43 @@
|
|
|
1
|
+
# PlainModel
|
|
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/plain_model`. 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 'plain_model'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install plain_model
|
|
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 test` 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/[USERNAME]/plain_model. 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.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the PlainModel project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/plain_model/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'plain_model'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainModel
|
|
4
|
+
class AssociationBuilder
|
|
5
|
+
attr_reader :model_class, :options
|
|
6
|
+
|
|
7
|
+
def initialize(model_class, options = {})
|
|
8
|
+
@model_class = model_class
|
|
9
|
+
@options = options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def load_records(name, records, context: nil, includes: [])
|
|
13
|
+
apply = options.fetch(:apply) { :"_records_for_#{name}" }
|
|
14
|
+
args = [records, context: context, includes: includes, association: name]
|
|
15
|
+
if apply.is_a?(Symbol)
|
|
16
|
+
model_class.public_send(apply, *args)
|
|
17
|
+
else
|
|
18
|
+
model_class.instance_exec(*args, &apply)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PlainModel
|
|
4
|
+
class MergeIncludes
|
|
5
|
+
def initialize(old_values)
|
|
6
|
+
@old_values = old_values
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def merge(new_values)
|
|
10
|
+
normalized = normalize_values(new_values)
|
|
11
|
+
@old_values.dup.deep_merge(normalized)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
# Converts includes into hash structure.
|
|
17
|
+
# In this structure key is a include name and value is a nested hash structure for this include.
|
|
18
|
+
# It allows to merge and parse includes with ease.
|
|
19
|
+
# example:
|
|
20
|
+
# new_values => [:foo, :baz, bar: :baz, qwe: [:asd, :zxc], foo: { baz: { asd: [:qwe] } }]
|
|
21
|
+
# normalize_values(new_values) =>
|
|
22
|
+
# {
|
|
23
|
+
# foo: {
|
|
24
|
+
# baz: {
|
|
25
|
+
# asd: {
|
|
26
|
+
# qwe: {}
|
|
27
|
+
# }
|
|
28
|
+
# }
|
|
29
|
+
# },
|
|
30
|
+
# bar: { baz: {} },
|
|
31
|
+
# baz: {},
|
|
32
|
+
# qwe: {
|
|
33
|
+
# asd: {},
|
|
34
|
+
# zxc: {}
|
|
35
|
+
# }
|
|
36
|
+
# }
|
|
37
|
+
def normalize_values(new_values)
|
|
38
|
+
new_values = Array.wrap(new_values)
|
|
39
|
+
return {} if new_values.empty? || (new_values.size == 1 && new_values.first == {})
|
|
40
|
+
|
|
41
|
+
normalized = {}
|
|
42
|
+
new_values.each do |value|
|
|
43
|
+
value = value.to_sym if value.is_a?(String)
|
|
44
|
+
value = { value => {} } unless value.is_a?(Hash)
|
|
45
|
+
value.each do |value_key, value_values|
|
|
46
|
+
value_key = value_key.to_sym if value_key.is_a?(String)
|
|
47
|
+
normalized[value_key] = normalized.fetch(value_key, {}).deep_merge(normalize_values(value_values))
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
normalized
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'modeling/base'
|
|
4
|
+
require_relative 'modeling/associatable'
|
|
5
|
+
require_relative 'modeling/queryable'
|
|
6
|
+
|
|
7
|
+
module PlainModel
|
|
8
|
+
class Model
|
|
9
|
+
extend PlainModel::Modeling::Queryable
|
|
10
|
+
|
|
11
|
+
include PlainModel::Modeling::Base
|
|
12
|
+
include PlainModel::Modeling::Associatable
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../association_builder'
|
|
4
|
+
require 'active_support/concern'
|
|
5
|
+
require 'active_support/core_ext/array/wrap'
|
|
6
|
+
|
|
7
|
+
module PlainModel
|
|
8
|
+
module Modeling
|
|
9
|
+
module Associatable
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
|
|
12
|
+
def associations
|
|
13
|
+
@associations ||= {}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
included do
|
|
17
|
+
class_attribute :association_types, instance_writer: false, default: {}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class_methods do
|
|
21
|
+
def define_association(name, options = {})
|
|
22
|
+
name = name.to_s
|
|
23
|
+
association_builder_class = options.fetch(:klass) { ::PlainModel::AssociationBuilder }
|
|
24
|
+
association_builder = association_builder_class.new self, options.except(:klass)
|
|
25
|
+
self.association_types = association_types.merge(name => association_builder)
|
|
26
|
+
define_association_methods(name)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def define_association_methods(name)
|
|
30
|
+
define_method(name) do
|
|
31
|
+
return associations[name] if association_loaded?(name)
|
|
32
|
+
|
|
33
|
+
associations[name] = load_association(name)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
define_method("#{name}=") do |value|
|
|
37
|
+
associations[name] = value
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def load_association(records, name, context: nil, includes: {})
|
|
42
|
+
association_builder = association_types.fetch(name.to_s) do
|
|
43
|
+
raise ArgumentError, "invalid association #{name}"
|
|
44
|
+
end
|
|
45
|
+
association_builder.load_records(name, records, context: context, includes: includes)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def load_associations(records, includes, context: nil)
|
|
49
|
+
includes.each do |name, nested|
|
|
50
|
+
load_association(records, name, context: context, includes: nested)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def association_loaded?(name)
|
|
56
|
+
associations.key?(name.to_s)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def load_association(name, context: nil, includes: {})
|
|
60
|
+
self.class.load_association(name, [self], context: context, includes: includes)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_model'
|
|
5
|
+
|
|
6
|
+
module PlainModel
|
|
7
|
+
module Modeling
|
|
8
|
+
module Base
|
|
9
|
+
Column = Struct.new(:name)
|
|
10
|
+
|
|
11
|
+
extend ActiveSupport::Concern
|
|
12
|
+
include ActiveModel::Model
|
|
13
|
+
include ActiveModel::Attributes
|
|
14
|
+
|
|
15
|
+
class_methods do
|
|
16
|
+
def column_names
|
|
17
|
+
return @column_names if defined?(@column_names)
|
|
18
|
+
|
|
19
|
+
@column_names = attribute_types.keys.map do |name|
|
|
20
|
+
Column.new(name: name.to_sym)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'forwardable'
|
|
5
|
+
|
|
6
|
+
module PlainModel
|
|
7
|
+
module Modeling
|
|
8
|
+
module Queryable
|
|
9
|
+
delegate :where, :includes, :except, to: :all
|
|
10
|
+
delegate :to_a, :first, :last, :each, :collect, :map, :select, :detect, to: :all
|
|
11
|
+
|
|
12
|
+
def _query_builder
|
|
13
|
+
::PlainModel::QueryBuilder.new(self)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def all
|
|
17
|
+
_query_builder
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'querying/base'
|
|
4
|
+
require_relative 'querying/with_model'
|
|
5
|
+
require_relative 'querying/except'
|
|
6
|
+
require_relative 'querying/where'
|
|
7
|
+
require_relative 'querying/includes'
|
|
8
|
+
|
|
9
|
+
module PlainModel
|
|
10
|
+
class QueryBuilder
|
|
11
|
+
include PlainModel::Querying::Base
|
|
12
|
+
include PlainModel::Querying::WithModel
|
|
13
|
+
include PlainModel::Querying::Except
|
|
14
|
+
include PlainModel::Querying::Where
|
|
15
|
+
include PlainModel::Querying::Includes
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/class/attribute'
|
|
5
|
+
require 'forwardable'
|
|
6
|
+
|
|
7
|
+
module PlainModel
|
|
8
|
+
module Querying
|
|
9
|
+
module Base
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
|
|
12
|
+
included do
|
|
13
|
+
class_attribute :chainable_methods, instance_accessor: false, default: []
|
|
14
|
+
|
|
15
|
+
class_attribute :result_methods,
|
|
16
|
+
instance_accessor: false,
|
|
17
|
+
default: [:to_a, :first, :last, :each, :collect, :map, :select, :detect]
|
|
18
|
+
|
|
19
|
+
extend Forwardable
|
|
20
|
+
attr_reader :values
|
|
21
|
+
instance_delegate [:first, :last, :each, :collect, :map, :select, :detect] => :all
|
|
22
|
+
private :_within_new_instance, :_records
|
|
23
|
+
|
|
24
|
+
def initialize(*args)
|
|
25
|
+
@values = {}
|
|
26
|
+
super(*args)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
protected
|
|
30
|
+
|
|
31
|
+
attr_writer :values
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def to_a
|
|
35
|
+
return @to_a if defined?(@to_a)
|
|
36
|
+
|
|
37
|
+
@to_a = _records
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def dup
|
|
41
|
+
new_instance = self.class.new(*dup_args)
|
|
42
|
+
new_instance.values = values.dup
|
|
43
|
+
new_instance
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def dup_args
|
|
47
|
+
[]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def _within_new_instance(&block)
|
|
51
|
+
new_instance = dup
|
|
52
|
+
new_instance.instance_exec(&block)
|
|
53
|
+
new_instance
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def _records
|
|
57
|
+
raise NotImplementedError, "implement #_records private method in #{self.class}"
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
|
|
5
|
+
module PlainModel
|
|
6
|
+
module Querying
|
|
7
|
+
module Except
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
# Chain method
|
|
11
|
+
# @param keys [Array<Symbol>] values keys that you want to exclude from query
|
|
12
|
+
# @return new instance with applied changes
|
|
13
|
+
def except(*keys)
|
|
14
|
+
_within_new_instance do
|
|
15
|
+
self.values = values.except(*keys)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
included do
|
|
20
|
+
self.chainable_methods += [:except]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
require 'active_support/core_ext/array/wrap'
|
|
5
|
+
require_relative '../merge_includes'
|
|
6
|
+
|
|
7
|
+
module PlainModel
|
|
8
|
+
module Querying
|
|
9
|
+
module Includes
|
|
10
|
+
extend ActiveSupport::Concern
|
|
11
|
+
|
|
12
|
+
def initialize(*args)
|
|
13
|
+
super(*args)
|
|
14
|
+
values[:includes] = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
included do
|
|
18
|
+
self.chainable_methods += [:includes]
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Chain method
|
|
22
|
+
# @param names [Array<Symbol,Hash>] - names of includes with optional tail hash for nested includes
|
|
23
|
+
# @return new instance with applied changes
|
|
24
|
+
def includes(*names)
|
|
25
|
+
_within_new_instance do
|
|
26
|
+
new_includes = ::PlainModel::MergeIncludes.new(values[:includes]).merge(names)
|
|
27
|
+
values[:includes] = new_includes
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
|
|
5
|
+
module PlainModel
|
|
6
|
+
module Querying
|
|
7
|
+
module Where
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
def initialize(*args)
|
|
11
|
+
super(*args)
|
|
12
|
+
values[:where] = {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
included do
|
|
16
|
+
self.chainable_methods += [:except]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Chain method
|
|
20
|
+
# @param conditions [Hash]
|
|
21
|
+
# @return new instance with applied changes
|
|
22
|
+
def where(conditions)
|
|
23
|
+
_within_new_instance do
|
|
24
|
+
values[:where].merge!(conditions)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'active_support/concern'
|
|
4
|
+
|
|
5
|
+
module PlainModel
|
|
6
|
+
module Querying
|
|
7
|
+
module WithModel
|
|
8
|
+
extend ActiveSupport::Concern
|
|
9
|
+
|
|
10
|
+
def initialize(model_class, *args)
|
|
11
|
+
@model_class = model_class
|
|
12
|
+
super(*args)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
included do
|
|
16
|
+
attr_reader :model_class
|
|
17
|
+
|
|
18
|
+
def dup_args
|
|
19
|
+
[model_class]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def _records
|
|
25
|
+
model_class._records(values)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/plain_model.rb
ADDED
data/plain_model.gemspec
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'plain_model/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'plain_model'
|
|
9
|
+
spec.version = PlainModel::VERSION
|
|
10
|
+
spec.authors = ['Denis Talakevich']
|
|
11
|
+
spec.email = ['senid231@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'Plan Old Ruby Object with ORM and query support.'
|
|
14
|
+
spec.description = 'Plan Old Ruby Object with ORM and query support.'
|
|
15
|
+
spec.homepage = 'https://github.com/senid231/plain_model'
|
|
16
|
+
spec.license = 'MIT'
|
|
17
|
+
|
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
22
|
+
end
|
|
23
|
+
spec.bindir = 'exe'
|
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
25
|
+
spec.require_paths = ['lib']
|
|
26
|
+
|
|
27
|
+
spec.add_dependency 'activemodel', '~> 5.2'
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
30
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.71.0'
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: plain_model
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Denis Talakevich
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-06-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activemodel
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '5.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '5.2'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '2.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '2.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
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: rubocop
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.71.0
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.71.0
|
|
83
|
+
description: Plan Old Ruby Object with ORM and query support.
|
|
84
|
+
email:
|
|
85
|
+
- senid231@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- ".gitignore"
|
|
91
|
+
- ".rubocop.yml"
|
|
92
|
+
- ".travis.yml"
|
|
93
|
+
- CODE_OF_CONDUCT.md
|
|
94
|
+
- Gemfile
|
|
95
|
+
- LICENSE.txt
|
|
96
|
+
- README.md
|
|
97
|
+
- Rakefile
|
|
98
|
+
- bin/console
|
|
99
|
+
- bin/setup
|
|
100
|
+
- lib/plain_model.rb
|
|
101
|
+
- lib/plain_model/association_builder.rb
|
|
102
|
+
- lib/plain_model/errors.rb
|
|
103
|
+
- lib/plain_model/merge_includes.rb
|
|
104
|
+
- lib/plain_model/model.rb
|
|
105
|
+
- lib/plain_model/modeling/associatable.rb
|
|
106
|
+
- lib/plain_model/modeling/base.rb
|
|
107
|
+
- lib/plain_model/modeling/queryable.rb
|
|
108
|
+
- lib/plain_model/query_builder.rb
|
|
109
|
+
- lib/plain_model/querying/base.rb
|
|
110
|
+
- lib/plain_model/querying/except.rb
|
|
111
|
+
- lib/plain_model/querying/includes.rb
|
|
112
|
+
- lib/plain_model/querying/where.rb
|
|
113
|
+
- lib/plain_model/querying/with_model.rb
|
|
114
|
+
- lib/plain_model/version.rb
|
|
115
|
+
- plain_model.gemspec
|
|
116
|
+
homepage: https://github.com/senid231/plain_model
|
|
117
|
+
licenses:
|
|
118
|
+
- MIT
|
|
119
|
+
metadata: {}
|
|
120
|
+
post_install_message:
|
|
121
|
+
rdoc_options: []
|
|
122
|
+
require_paths:
|
|
123
|
+
- lib
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
requirements:
|
|
126
|
+
- - ">="
|
|
127
|
+
- !ruby/object:Gem::Version
|
|
128
|
+
version: '0'
|
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
|
+
requirements:
|
|
131
|
+
- - ">="
|
|
132
|
+
- !ruby/object:Gem::Version
|
|
133
|
+
version: '0'
|
|
134
|
+
requirements: []
|
|
135
|
+
rubyforge_project:
|
|
136
|
+
rubygems_version: 2.7.9
|
|
137
|
+
signing_key:
|
|
138
|
+
specification_version: 4
|
|
139
|
+
summary: Plan Old Ruby Object with ORM and query support.
|
|
140
|
+
test_files: []
|