plain_model 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbfb75e773e360168629f706d2180999618cb6bd9e1021be975beafa22e5106c
4
- data.tar.gz: fd281798e57e733940630ed0c930ed83a8b6d66562f04d7aaf541d2100a8b095
3
+ metadata.gz: e7edc77ecf12c7fe60433f63f23d795b49d99159c3565d848e49cc48b17ebb26
4
+ data.tar.gz: a565e4f7f13b9d8a3974d56a2da1f8ed8956bb8af9f5b57d841d43c1be7c0940
5
5
  SHA512:
6
- metadata.gz: a65be93c437fd1e95933448aac1b74ea859c2db7e573ce4ff56689ea8c603d6f8b34d93a03673183ccdb63c2055b6661cd2e0742b946b66d53a56b3c6fc8b8b2
7
- data.tar.gz: 126876f5467e55745540232a77e566cf99366a13be74c015298da16cb48b07503920f80869e1ed45775eef5f45c8553c250ca6440c94e83f0abe8a21664905ae
6
+ metadata.gz: 799deefe0bc47e69a0a2ac89c01f4e2e4c1950b4f06f4edf4248d7aac47c79bef07a3a18ce96bb16cbf17a8798d55a5568a62f3ee02e36d1b1b71e69e7c15f51
7
+ data.tar.gz: 2ece3a004d52ab5dc6af1bc7b4b03c8b5e8549926cd9f6a1498550e23241d118ba7edae3904489efcd707754119476d0b59efec1104e0839328f25c494c7b9bd
@@ -9,9 +9,9 @@ require_relative 'querying/includes'
9
9
  module PlainModel
10
10
  class QueryBuilder
11
11
  include PlainModel::Querying::Base
12
- include PlainModel::Querying::WithModel
13
- include PlainModel::Querying::Except
14
12
  include PlainModel::Querying::Where
15
13
  include PlainModel::Querying::Includes
14
+ include PlainModel::Querying::Except
15
+ include PlainModel::Querying::WithModel
16
16
  end
17
17
  end
@@ -10,25 +10,19 @@ module PlainModel
10
10
  extend ActiveSupport::Concern
11
11
 
12
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
13
  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
14
+ attr_accessor :values
15
+ instance_delegate [:first, :last, :each, :collect, :map, :filter, :detect] => :to_a
16
+ private :_records
17
+ end
28
18
 
29
- protected
19
+ def initialize(*args)
20
+ @values = initial_values
21
+ super(*args)
22
+ end
30
23
 
31
- attr_writer :values
24
+ def initial_values
25
+ {}
32
26
  end
33
27
 
34
28
  def to_a
@@ -47,12 +41,6 @@ module PlainModel
47
41
  []
48
42
  end
49
43
 
50
- def _within_new_instance(&block)
51
- new_instance = dup
52
- new_instance.instance_exec(&block)
53
- new_instance
54
- end
55
-
56
44
  def _records
57
45
  raise NotImplementedError, "implement #_records private method in #{self.class}"
58
46
  end
@@ -11,14 +11,17 @@ module PlainModel
11
11
  # @param keys [Array<Symbol>] values keys that you want to exclude from query
12
12
  # @return new instance with applied changes
13
13
  def except(*keys)
14
- _within_new_instance do
15
- self.values = values.except(*keys)
16
- end
14
+ dup.except!(*keys)
17
15
  end
18
16
 
19
- included do
20
- self.chainable_methods += [:except]
17
+ # Chain method
18
+ # @param keys [Array<Symbol>] values keys that you want to exclude from query
19
+ # @return new instance with applied changes
20
+ def except!(*keys)
21
+ self.values = values.except(*keys)
22
+ self
21
23
  end
24
+
22
25
  end
23
26
  end
24
27
  end
@@ -9,23 +9,24 @@ module PlainModel
9
9
  module Includes
10
10
  extend ActiveSupport::Concern
11
11
 
12
- def initialize(*args)
13
- super(*args)
14
- values[:includes] = {}
15
- end
16
-
17
- included do
18
- self.chainable_methods += [:includes]
12
+ def initial_values
13
+ super.merge includes: {}
19
14
  end
20
15
 
21
16
  # Chain method
22
17
  # @param names [Array<Symbol,Hash>] - names of includes with optional tail hash for nested includes
23
18
  # @return new instance with applied changes
24
19
  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
20
+ dup.includes!(*names)
21
+ end
22
+
23
+ # Chain method
24
+ # @param names [Array<Symbol,Hash>] - names of includes with optional tail hash for nested includes
25
+ # @return current instance with applied changes
26
+ def includes!(*names)
27
+ new_includes = ::PlainModel::MergeIncludes.new(values[:includes]).merge(names)
28
+ values[:includes] = new_includes
29
+ self
29
30
  end
30
31
  end
31
32
  end
@@ -7,22 +7,23 @@ module PlainModel
7
7
  module Where
8
8
  extend ActiveSupport::Concern
9
9
 
10
- def initialize(*args)
11
- super(*args)
12
- values[:where] = {}
10
+ def initial_values
11
+ super.merge where: []
13
12
  end
14
13
 
15
- included do
16
- self.chainable_methods += [:except]
14
+ # Chain method
15
+ # @param conditions [Array]
16
+ # @return new instance with applied changes
17
+ def where(*conditions)
18
+ dup.where!(*conditions)
17
19
  end
18
20
 
19
21
  # 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
22
+ # @param conditions [Array]
23
+ # @return current instance with applied changes
24
+ def where!(*conditions)
25
+ values[:where] = (values[:where] + conditions).uniq
26
+ self
26
27
  end
27
28
  end
28
29
  end
@@ -7,24 +7,24 @@ module PlainModel
7
7
  module WithModel
8
8
  extend ActiveSupport::Concern
9
9
 
10
+ included do
11
+ attr_reader :model_class
12
+ private :_records
13
+ end
14
+
10
15
  def initialize(model_class, *args)
11
16
  @model_class = model_class
12
17
  super(*args)
13
18
  end
14
19
 
15
- included do
16
- attr_reader :model_class
17
-
18
- def dup_args
19
- [model_class]
20
- end
21
-
22
- private
20
+ def dup_args
21
+ super + [model_class]
22
+ end
23
23
 
24
- def _records
25
- model_class._records(values)
26
- end
24
+ def _records
25
+ model_class._records(values)
27
26
  end
27
+
28
28
  end
29
29
  end
30
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PlainModel
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plain_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-14 00:00:00.000000000 Z
11
+ date: 2019-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -132,8 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.7.9
135
+ rubygems_version: 3.0.4
137
136
  signing_key:
138
137
  specification_version: 4
139
138
  summary: Plan Old Ruby Object with ORM and query support.