jsonb_accessor 1.0.0.beta.6 → 1.0.0.beta.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/gemfiles/activerecord_5.0.0.gemfile.lock +3 -3
- data/gemfiles/activerecord_5.1.0.gemfile.lock +3 -3
- data/jsonb_accessor.gemspec +1 -1
- data/lib/jsonb_accessor.rb +1 -0
- data/lib/jsonb_accessor/macro.rb +12 -5
- data/lib/jsonb_accessor/query_builder.rb +19 -127
- data/lib/jsonb_accessor/query_helper.rb +114 -0
- data/lib/jsonb_accessor/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9443b5a6ade6928899a6d286c8302a95bab56c9c
|
4
|
+
data.tar.gz: 5168b13af14c34bfd05652f9dab3099ef0b555bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d0dfee7666cf5d0390faa6205813152f3c1ef013ff26864d64959cb5eaf9bb099784677d870f04176ff30b089b0c247b21361688b71d34c6c0808caa4fddbd8
|
7
|
+
data.tar.gz: 887584f334980e0c03866f443090db22c1d9dcd3ffc360c2733a7d133388aed25ed3f73438ba33a9cba35a71468b959fb9fc6e8ded1cb4fe7bf81e1479cf11af
|
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
jsonb_accessor (1.0.0.beta.
|
4
|
+
jsonb_accessor (1.0.0.beta.7)
|
5
5
|
activerecord (>= 5.0)
|
6
6
|
activesupport (>= 5.0)
|
7
7
|
pg (>= 0.18.1)
|
@@ -123,7 +123,7 @@ DEPENDENCIES
|
|
123
123
|
activerecord (~> 5.0.0)
|
124
124
|
appraisal (~> 2.2.0)
|
125
125
|
awesome_print
|
126
|
-
bundler (~> 1.
|
126
|
+
bundler (~> 1.15.0)
|
127
127
|
database_cleaner (~> 1.6.0)
|
128
128
|
jsonb_accessor!
|
129
129
|
pg
|
@@ -136,4 +136,4 @@ DEPENDENCIES
|
|
136
136
|
standalone_migrations (~> 5.2.0)
|
137
137
|
|
138
138
|
BUNDLED WITH
|
139
|
-
1.
|
139
|
+
1.15.3
|
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ..
|
3
3
|
specs:
|
4
|
-
jsonb_accessor (1.0.0.beta.
|
4
|
+
jsonb_accessor (1.0.0.beta.7)
|
5
5
|
activerecord (>= 5.0)
|
6
6
|
activesupport (>= 5.0)
|
7
7
|
pg (>= 0.18.1)
|
@@ -123,7 +123,7 @@ DEPENDENCIES
|
|
123
123
|
activerecord (~> 5.1.0)
|
124
124
|
appraisal (~> 2.2.0)
|
125
125
|
awesome_print
|
126
|
-
bundler (~> 1.
|
126
|
+
bundler (~> 1.15.0)
|
127
127
|
database_cleaner (~> 1.6.0)
|
128
128
|
jsonb_accessor!
|
129
129
|
pg
|
@@ -136,4 +136,4 @@ DEPENDENCIES
|
|
136
136
|
standalone_migrations (~> 5.2.0)
|
137
137
|
|
138
138
|
BUNDLED WITH
|
139
|
-
1.
|
139
|
+
1.15.3
|
data/jsonb_accessor.gemspec
CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_dependency "pg", ">= 0.18.1"
|
28
28
|
|
29
29
|
spec.add_development_dependency "appraisal", "~> 2.2.0"
|
30
|
-
spec.add_development_dependency "bundler", "~> 1.
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.15.0"
|
31
31
|
spec.add_development_dependency "database_cleaner", "~> 1.6.0"
|
32
32
|
spec.add_development_dependency "awesome_print"
|
33
33
|
spec.add_development_dependency "pry"
|
data/lib/jsonb_accessor.rb
CHANGED
data/lib/jsonb_accessor/macro.rb
CHANGED
@@ -34,7 +34,7 @@ module JsonbAccessor
|
|
34
34
|
end
|
35
35
|
|
36
36
|
# Get store keys to default values mapping
|
37
|
-
store_keys_and_defaults = ::JsonbAccessor::
|
37
|
+
store_keys_and_defaults = ::JsonbAccessor::QueryHelper.convert_keys_to_store_keys(names_and_defaults, public_send(store_key_mapping_method_name))
|
38
38
|
|
39
39
|
# Define jsonb_defaults_mapping_for_<jsonb_attribute>
|
40
40
|
defaults_mapping_method_name = "jsonb_defaults_mapping_for_#{jsonb_attribute}"
|
@@ -46,7 +46,14 @@ module JsonbAccessor
|
|
46
46
|
end
|
47
47
|
|
48
48
|
all_defaults_mapping = public_send(defaults_mapping_method_name)
|
49
|
-
|
49
|
+
# Fields may have procs as default value. This means `all_defaults_mapping` may contain procs as values. To make this work
|
50
|
+
# with the attributes API, we need to wrap `all_defaults_mapping` with a proc itself, making sure it returns a plain hash
|
51
|
+
# each time it is evaluated.
|
52
|
+
all_defaults_mapping_proc =
|
53
|
+
if all_defaults_mapping.present?
|
54
|
+
-> { all_defaults_mapping.map { |key, value| [key, value.respond_to?(:call) ? value.call : value] }.to_h }
|
55
|
+
end
|
56
|
+
attribute jsonb_attribute, :jsonb, default: all_defaults_mapping_proc if all_defaults_mapping_proc.present?
|
50
57
|
|
51
58
|
# Setters are in a module to allow users to override them and still be able to use `super`.
|
52
59
|
setters = Module.new do
|
@@ -67,7 +74,7 @@ module JsonbAccessor
|
|
67
74
|
empty_store_key_attributes = names_to_store_keys.values.each_with_object({}) { |name, defaults| defaults[name] = nil }
|
68
75
|
empty_named_attributes = names_to_store_keys.keys.each_with_object({}) { |name, defaults| defaults[name] = nil }
|
69
76
|
|
70
|
-
store_key_attributes = ::JsonbAccessor::
|
77
|
+
store_key_attributes = ::JsonbAccessor::QueryHelper.convert_keys_to_store_keys(value, names_to_store_keys)
|
71
78
|
write_attribute(jsonb_attribute, empty_store_key_attributes.merge(store_key_attributes))
|
72
79
|
|
73
80
|
empty_named_attributes.merge(value).each { |name, attribute_value| write_attribute(name, attribute_value) }
|
@@ -89,13 +96,13 @@ module JsonbAccessor
|
|
89
96
|
|
90
97
|
# <jsonb_attribute>_where scope
|
91
98
|
scope("#{jsonb_attribute}_where", lambda do |attributes|
|
92
|
-
store_key_attributes = ::JsonbAccessor::
|
99
|
+
store_key_attributes = ::JsonbAccessor::QueryHelper.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
|
93
100
|
jsonb_where(jsonb_attribute, store_key_attributes)
|
94
101
|
end)
|
95
102
|
|
96
103
|
# <jsonb_attribute>_where_not scope
|
97
104
|
scope("#{jsonb_attribute}_where_not", lambda do |attributes|
|
98
|
-
store_key_attributes = ::JsonbAccessor::
|
105
|
+
store_key_attributes = ::JsonbAccessor::QueryHelper.convert_keys_to_store_keys(attributes, all.model.public_send(store_key_mapping_method_name))
|
99
106
|
jsonb_where_not(jsonb_attribute, store_key_attributes)
|
100
107
|
end)
|
101
108
|
|
@@ -1,147 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module JsonbAccessor
|
4
|
-
GREATER_THAN = ">"
|
5
|
-
GREATER_THAN_OR_EQUAL_TO = ">="
|
6
|
-
LESS_THAN = "<"
|
7
|
-
LESS_THAN_OR_EQUAL_TO = "<="
|
8
|
-
|
9
|
-
NUMBER_OPERATORS_MAP = {
|
10
|
-
GREATER_THAN => GREATER_THAN,
|
11
|
-
"greater_than" => GREATER_THAN,
|
12
|
-
"gt" => GREATER_THAN,
|
13
|
-
GREATER_THAN_OR_EQUAL_TO => GREATER_THAN_OR_EQUAL_TO,
|
14
|
-
"greater_than_or_equal_to" => GREATER_THAN_OR_EQUAL_TO,
|
15
|
-
"gte" => GREATER_THAN_OR_EQUAL_TO,
|
16
|
-
LESS_THAN => LESS_THAN,
|
17
|
-
"less_than" => LESS_THAN,
|
18
|
-
"lt" => LESS_THAN,
|
19
|
-
LESS_THAN_OR_EQUAL_TO => LESS_THAN_OR_EQUAL_TO,
|
20
|
-
"less_than_or_equal_to" => LESS_THAN_OR_EQUAL_TO,
|
21
|
-
"lte" => LESS_THAN_OR_EQUAL_TO
|
22
|
-
}.freeze
|
23
|
-
|
24
|
-
NUMBER_OPERATORS = NUMBER_OPERATORS_MAP.keys.freeze
|
25
|
-
|
26
|
-
TIME_OPERATORS_MAP = {
|
27
|
-
"after" => GREATER_THAN,
|
28
|
-
"before" => LESS_THAN
|
29
|
-
}.freeze
|
30
|
-
|
31
|
-
TIME_OPERATORS = TIME_OPERATORS_MAP.keys.freeze
|
32
|
-
|
33
|
-
IS_NUMBER_QUERY_ARGUMENTS = lambda do |arg|
|
34
|
-
arg.is_a?(Hash) &&
|
35
|
-
arg.keys.map(&:to_s).all? { |key| JsonbAccessor::NUMBER_OPERATORS.include?(key) }
|
36
|
-
end
|
37
|
-
|
38
|
-
IS_TIME_QUERY_ARGUMENTS = lambda do |arg|
|
39
|
-
arg.is_a?(Hash) &&
|
40
|
-
arg.keys.map(&:to_s).all? { |key| JsonbAccessor::TIME_OPERATORS.include?(key) }
|
41
|
-
end
|
42
|
-
|
43
|
-
CONVERT_NUMBER_RANGES = lambda do |attributes|
|
44
|
-
attributes.each_with_object({}) do |(name, value), new_attributes|
|
45
|
-
is_range = value.is_a?(Range)
|
46
|
-
|
47
|
-
new_attributes[name] = if is_range && value.first.is_a?(Numeric) && value.exclude_end?
|
48
|
-
{ greater_than_or_equal_to: value.first, less_than: value.end }
|
49
|
-
elsif is_range && value.first.is_a?(Numeric)
|
50
|
-
{ greater_than_or_equal_to: value.first, less_than_or_equal_to: value.end }
|
51
|
-
else
|
52
|
-
value
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
CONVERT_TIME_RANGES = lambda do |attributes|
|
58
|
-
attributes.each_with_object({}) do |(name, value), new_attributes|
|
59
|
-
is_range = value.is_a?(Range)
|
60
|
-
|
61
|
-
if is_range && (value.first.is_a?(Time) || value.first.is_a?(Date))
|
62
|
-
start_time = value.first
|
63
|
-
end_time = value.end
|
64
|
-
new_attributes[name] = { before: end_time, after: start_time }
|
65
|
-
else
|
66
|
-
new_attributes[name] = value
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
CONVERT_RANGES = lambda do |attributes|
|
72
|
-
[CONVERT_NUMBER_RANGES, CONVERT_TIME_RANGES].reduce(attributes) do |new_attributes, converter|
|
73
|
-
converter.call(new_attributes)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
ORDER_DIRECTIONS = [:asc, :desc, "asc", "desc"].freeze
|
78
|
-
|
79
4
|
module QueryBuilder
|
80
5
|
extend ActiveSupport::Concern
|
81
|
-
InvalidColumnName = Class.new(StandardError)
|
82
|
-
InvalidFieldName = Class.new(StandardError)
|
83
|
-
InvalidDirection = Class.new(StandardError)
|
84
|
-
NotSupported = Class.new(StandardError)
|
85
|
-
|
86
|
-
def self.validate_column_name!(query, column_name)
|
87
|
-
if query.model.columns.none? { |column| column.name == column_name.to_s }
|
88
|
-
raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table"
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def self.validate_field_name!(query, column_name, field_name)
|
93
|
-
store_keys = query.model.public_send("jsonb_store_key_mapping_for_#{column_name}").values
|
94
|
-
if store_keys.exclude?(field_name.to_s)
|
95
|
-
valid_field_names = store_keys.map { |key| "`#{key}`" }.join(", ")
|
96
|
-
raise InvalidFieldName, "`#{field_name}` is not a valid field name, valid field names include: #{valid_field_names}"
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def self.validate_direction!(option)
|
101
|
-
if ORDER_DIRECTIONS.exclude?(option)
|
102
|
-
raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted"
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def self.convert_keys_to_store_keys(attributes, store_key_mapping)
|
107
|
-
attributes.each_with_object({}) do |(name, value), new_attributes|
|
108
|
-
store_key = store_key_mapping[name.to_s]
|
109
|
-
new_attributes[store_key] = value
|
110
|
-
end
|
111
|
-
end
|
112
6
|
|
113
7
|
included do
|
114
8
|
scope(:jsonb_contains, lambda do |column_name, attributes|
|
115
|
-
JsonbAccessor::
|
9
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
116
10
|
where("#{table_name}.#{column_name} @> (?)::jsonb", attributes.to_json)
|
117
11
|
end)
|
118
12
|
|
119
13
|
scope(:jsonb_excludes, lambda do |column_name, attributes|
|
120
|
-
JsonbAccessor::
|
14
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
121
15
|
where.not("#{table_name}.#{column_name} @> (?)::jsonb", attributes.to_json)
|
122
16
|
end)
|
123
17
|
|
124
18
|
scope(:jsonb_number_where, lambda do |column_name, field_name, given_operator, value|
|
125
|
-
JsonbAccessor::
|
126
|
-
operator = JsonbAccessor::NUMBER_OPERATORS_MAP.fetch(given_operator.to_s)
|
19
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
20
|
+
operator = JsonbAccessor::QueryHelper::NUMBER_OPERATORS_MAP.fetch(given_operator.to_s)
|
127
21
|
where("(#{table_name}.#{column_name} ->> ?)::float #{operator} ?", field_name, value)
|
128
22
|
end)
|
129
23
|
|
130
24
|
scope(:jsonb_number_where_not, lambda do |column_name, field_name, given_operator, value|
|
131
|
-
JsonbAccessor::
|
132
|
-
operator = JsonbAccessor::NUMBER_OPERATORS_MAP.fetch(given_operator.to_s)
|
25
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
26
|
+
operator = JsonbAccessor::QueryHelper::NUMBER_OPERATORS_MAP.fetch(given_operator.to_s)
|
133
27
|
where.not("(#{table_name}.#{column_name} ->> ?)::float #{operator} ?", field_name, value)
|
134
28
|
end)
|
135
29
|
|
136
30
|
scope(:jsonb_time_where, lambda do |column_name, field_name, given_operator, value|
|
137
|
-
JsonbAccessor::
|
138
|
-
operator = JsonbAccessor::TIME_OPERATORS_MAP.fetch(given_operator.to_s)
|
31
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
32
|
+
operator = JsonbAccessor::QueryHelper::TIME_OPERATORS_MAP.fetch(given_operator.to_s)
|
139
33
|
where("(#{table_name}.#{column_name} ->> ?)::timestamp #{operator} ?", field_name, value)
|
140
34
|
end)
|
141
35
|
|
142
36
|
scope(:jsonb_time_where_not, lambda do |column_name, field_name, given_operator, value|
|
143
|
-
JsonbAccessor::
|
144
|
-
operator = JsonbAccessor::TIME_OPERATORS_MAP.fetch(given_operator.to_s)
|
37
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
38
|
+
operator = JsonbAccessor::QueryHelper::TIME_OPERATORS_MAP.fetch(given_operator.to_s)
|
145
39
|
where.not("(#{table_name}.#{column_name} ->> ?)::timestamp #{operator} ?", field_name, value)
|
146
40
|
end)
|
147
41
|
|
@@ -149,11 +43,10 @@ module JsonbAccessor
|
|
149
43
|
query = all
|
150
44
|
contains_attributes = {}
|
151
45
|
|
152
|
-
JsonbAccessor::
|
153
|
-
|
154
|
-
when IS_NUMBER_QUERY_ARGUMENTS
|
46
|
+
JsonbAccessor::QueryHelper.convert_ranges(attributes).each do |name, value|
|
47
|
+
if JsonbAccessor::QueryHelper.number_query_arguments?(value)
|
155
48
|
value.each { |operator, query_value| query = query.jsonb_number_where(column_name, name, operator, query_value) }
|
156
|
-
|
49
|
+
elsif JsonbAccessor::QueryHelper.time_query_arguments?(value)
|
157
50
|
value.each { |operator, query_value| query = query.jsonb_time_where(column_name, name, operator, query_value) }
|
158
51
|
else
|
159
52
|
contains_attributes[name] = value
|
@@ -169,13 +62,12 @@ module JsonbAccessor
|
|
169
62
|
|
170
63
|
attributes.each do |name, value|
|
171
64
|
if value.is_a?(Range)
|
172
|
-
raise NotSupported, "`jsonb_where_not` scope does not accept ranges as arguments. Given `#{value}` for `#{name}` field"
|
65
|
+
raise JsonbAccessor::QueryHelper::NotSupported, "`jsonb_where_not` scope does not accept ranges as arguments. Given `#{value}` for `#{name}` field"
|
173
66
|
end
|
174
67
|
|
175
|
-
|
176
|
-
when IS_NUMBER_QUERY_ARGUMENTS
|
68
|
+
if JsonbAccessor::QueryHelper.number_query_arguments?(value)
|
177
69
|
value.each { |operator, query_value| query = query.jsonb_number_where_not(column_name, name, operator, query_value) }
|
178
|
-
|
70
|
+
elsif JsonbAccessor::QueryHelper.time_query_arguments?(value)
|
179
71
|
value.each { |operator, query_value| query = query.jsonb_time_where_not(column_name, name, operator, query_value) }
|
180
72
|
else
|
181
73
|
excludes_attributes[name] = value
|
@@ -186,9 +78,9 @@ module JsonbAccessor
|
|
186
78
|
end)
|
187
79
|
|
188
80
|
scope(:jsonb_order, lambda do |column_name, field_name, direction|
|
189
|
-
JsonbAccessor::
|
190
|
-
JsonbAccessor::
|
191
|
-
JsonbAccessor::
|
81
|
+
JsonbAccessor::QueryHelper.validate_column_name!(all, column_name)
|
82
|
+
JsonbAccessor::QueryHelper.validate_field_name!(all, column_name, field_name)
|
83
|
+
JsonbAccessor::QueryHelper.validate_direction!(direction)
|
192
84
|
order("(#{table_name}.#{column_name} -> '#{field_name}') #{direction}")
|
193
85
|
end)
|
194
86
|
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonbAccessor
|
4
|
+
module QueryHelper
|
5
|
+
# Errors
|
6
|
+
InvalidColumnName = Class.new(StandardError)
|
7
|
+
InvalidFieldName = Class.new(StandardError)
|
8
|
+
InvalidDirection = Class.new(StandardError)
|
9
|
+
NotSupported = Class.new(StandardError)
|
10
|
+
|
11
|
+
# Constants
|
12
|
+
GREATER_THAN = ">"
|
13
|
+
GREATER_THAN_OR_EQUAL_TO = ">="
|
14
|
+
LESS_THAN = "<"
|
15
|
+
LESS_THAN_OR_EQUAL_TO = "<="
|
16
|
+
|
17
|
+
NUMBER_OPERATORS_MAP = {
|
18
|
+
GREATER_THAN => GREATER_THAN,
|
19
|
+
"greater_than" => GREATER_THAN,
|
20
|
+
"gt" => GREATER_THAN,
|
21
|
+
GREATER_THAN_OR_EQUAL_TO => GREATER_THAN_OR_EQUAL_TO,
|
22
|
+
"greater_than_or_equal_to" => GREATER_THAN_OR_EQUAL_TO,
|
23
|
+
"gte" => GREATER_THAN_OR_EQUAL_TO,
|
24
|
+
LESS_THAN => LESS_THAN,
|
25
|
+
"less_than" => LESS_THAN,
|
26
|
+
"lt" => LESS_THAN,
|
27
|
+
LESS_THAN_OR_EQUAL_TO => LESS_THAN_OR_EQUAL_TO,
|
28
|
+
"less_than_or_equal_to" => LESS_THAN_OR_EQUAL_TO,
|
29
|
+
"lte" => LESS_THAN_OR_EQUAL_TO
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
NUMBER_OPERATORS = NUMBER_OPERATORS_MAP.keys.freeze
|
33
|
+
|
34
|
+
TIME_OPERATORS_MAP = {
|
35
|
+
"after" => GREATER_THAN,
|
36
|
+
"before" => LESS_THAN
|
37
|
+
}.freeze
|
38
|
+
|
39
|
+
TIME_OPERATORS = TIME_OPERATORS_MAP.keys.freeze
|
40
|
+
|
41
|
+
ORDER_DIRECTIONS = [:asc, :desc, "asc", "desc"].freeze
|
42
|
+
|
43
|
+
class << self
|
44
|
+
def validate_column_name!(query, column_name)
|
45
|
+
if query.model.columns.none? { |column| column.name == column_name.to_s }
|
46
|
+
raise InvalidColumnName, "a column named `#{column_name}` does not exist on the `#{query.model.table_name}` table"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_field_name!(query, column_name, field_name)
|
51
|
+
store_keys = query.model.public_send("jsonb_store_key_mapping_for_#{column_name}").values
|
52
|
+
if store_keys.exclude?(field_name.to_s)
|
53
|
+
valid_field_names = store_keys.map { |key| "`#{key}`" }.join(", ")
|
54
|
+
raise InvalidFieldName, "`#{field_name}` is not a valid field name, valid field names include: #{valid_field_names}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def validate_direction!(option)
|
59
|
+
if ORDER_DIRECTIONS.exclude?(option)
|
60
|
+
raise InvalidDirection, "`#{option}` is not a valid direction for ordering, only `asc` and `desc` are accepted"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def convert_keys_to_store_keys(attributes, store_key_mapping)
|
65
|
+
attributes.each_with_object({}) do |(name, value), new_attributes|
|
66
|
+
store_key = store_key_mapping[name.to_s]
|
67
|
+
new_attributes[store_key] = value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def number_query_arguments?(arg)
|
72
|
+
arg.is_a?(Hash) && arg.keys.map(&:to_s).all? { |key| NUMBER_OPERATORS.include?(key) }
|
73
|
+
end
|
74
|
+
|
75
|
+
def time_query_arguments?(arg)
|
76
|
+
arg.is_a?(Hash) && arg.keys.map(&:to_s).all? { |key| TIME_OPERATORS.include?(key) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def convert_number_ranges(attributes)
|
80
|
+
attributes.each_with_object({}) do |(name, value), new_attributes|
|
81
|
+
is_range = value.is_a?(Range)
|
82
|
+
|
83
|
+
new_attributes[name] = if is_range && value.first.is_a?(Numeric) && value.exclude_end?
|
84
|
+
{ greater_than_or_equal_to: value.first, less_than: value.end }
|
85
|
+
elsif is_range && value.first.is_a?(Numeric)
|
86
|
+
{ greater_than_or_equal_to: value.first, less_than_or_equal_to: value.end }
|
87
|
+
else
|
88
|
+
value
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def convert_time_ranges(attributes)
|
94
|
+
attributes.each_with_object({}) do |(name, value), new_attributes|
|
95
|
+
is_range = value.is_a?(Range)
|
96
|
+
|
97
|
+
if is_range && (value.first.is_a?(Time) || value.first.is_a?(Date))
|
98
|
+
start_time = value.first
|
99
|
+
end_time = value.end
|
100
|
+
new_attributes[name] = { before: end_time, after: start_time }
|
101
|
+
else
|
102
|
+
new_attributes[name] = value
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def convert_ranges(attributes)
|
108
|
+
%i[convert_number_ranges convert_time_ranges].reduce(attributes) do |new_attributes, converter_method|
|
109
|
+
public_send(converter_method, new_attributes)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonb_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.beta.
|
4
|
+
version: 1.0.0.beta.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Crismali
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-
|
13
|
+
date: 2017-08-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -74,14 +74,14 @@ dependencies:
|
|
74
74
|
requirements:
|
75
75
|
- - "~>"
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
77
|
+
version: 1.15.0
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
82
|
- - "~>"
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version:
|
84
|
+
version: 1.15.0
|
85
85
|
- !ruby/object:Gem::Dependency
|
86
86
|
name: database_cleaner
|
87
87
|
requirement: !ruby/object:Gem::Requirement
|
@@ -242,6 +242,7 @@ files:
|
|
242
242
|
- lib/jsonb_accessor.rb
|
243
243
|
- lib/jsonb_accessor/macro.rb
|
244
244
|
- lib/jsonb_accessor/query_builder.rb
|
245
|
+
- lib/jsonb_accessor/query_helper.rb
|
245
246
|
- lib/jsonb_accessor/version.rb
|
246
247
|
homepage: https://github.com/devmynd/jsonb_accessor
|
247
248
|
licenses:
|