hadouken-json 0.0.2 → 0.0.3

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: f8e63f5c3be2ce4a0fad68df7ea865de02aadeb62d5029387ced2bb11dfbc9d0
4
- data.tar.gz: b3edbc69aa4d3119b50aea249b42f445a7e49972855d29bf69603c172d1e3cdd
3
+ metadata.gz: 62fef84a45118f2f47cbd64a3634580aac3a563a172badedc5cc546afd560e54
4
+ data.tar.gz: a9350818a683f0b13f8ec0cdc1ed7b0f3c8d4d3932968713669f9382344977e8
5
5
  SHA512:
6
- metadata.gz: b3642a80fca4ad9d38563648904a4ed662773b28683f3e7db7eefc094278386b7201de8cb0223d46ccd1314cdc868d009dc19afc07505fa1bf9d4e164164dc06
7
- data.tar.gz: b54e1bb78accbb5a4216c187500c43365d697c87d9688ea00b97c1cdf0122972d20e2b2608842a584f3427febd84bb3417d5c0f108e85b0cd7b4eaf78b69a49d
6
+ metadata.gz: 915c7205d38b350c6120eac7cb4aaec445e79d70601143ec08fdbeb2866b56e3aa23bd27520273b84bb7f5a8c57ba1b08eee46596d030f1e0bca64fc134dcce8
7
+ data.tar.gz: 20cd69fb5a7045761e111c630037caca265f89dba328e352895bf801079d0ce75789e61197458255f5526f9ee169d6ca484c105f28e8e9d76acf40306e2329a4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Roman
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/lib/hadouken/json.rb CHANGED
@@ -18,7 +18,7 @@ class Hadouken::Json
18
18
  options = args[0] || {}
19
19
 
20
20
  Hadouken::SqlBuilder.call(
21
- main_class: relation.klass,
21
+ main_class: (relation_for(options[:jsonb_field_from]) || relation).klass,
22
22
  scope: options[:for],
23
23
  schema: json_schema,
24
24
  decorator: (options[:decorate_with].is_a?(Hadouken::Decorator) ? options[:decorate_with] : nil)
@@ -27,6 +27,12 @@ class Hadouken::Json
27
27
 
28
28
  private
29
29
 
30
+ def relation_for(scope)
31
+ return unless scope
32
+
33
+ scope.is_a?(ActiveRecord::Relation) ? scope : relation.klass.new(id: 1).instance_eval(scope)
34
+ end
35
+
30
36
  def execute_query(sql_query)
31
37
  ActiveRecord::Base.connection.execute(sql_query).values.first.first
32
38
  end
@@ -5,6 +5,7 @@ class Hadouken::SqlBuilder
5
5
  attribute :scope
6
6
  attribute :schema, Hash, default: {}
7
7
  attribute :decorator, Hadouken::Decorator
8
+
8
9
  def self.call(*args)
9
10
  new(*args).call
10
11
  end
@@ -49,6 +50,7 @@ class Hadouken::SqlBuilder
49
50
 
50
51
  def build_relation
51
52
  fail 'Scope should be ActiveRecord::Relation or string' if [ActiveRecord::Relation, String].none? { |klass| scope.is_a?( klass ) }
53
+ return main_class.from(unwound_jsonb_table) if scope_is_jsonb_array?
52
54
 
53
55
  scope.is_a?(ActiveRecord::Relation) ? scope : main_class.new(id: sample_id).instance_eval(scope)
54
56
  end
@@ -57,7 +59,7 @@ class Hadouken::SqlBuilder
57
59
  schema.extract!(*schema.select{ |_,v| v.is_a?(String) }.keys)
58
60
  .inject({}) do |h, (field, column)|
59
61
  col = (@relation&.klass&.column_names||[]).include?(column) ? [@relation.klass.table_name, column].join('.') : column
60
- h.merge(field => col)
62
+ scope_is_jsonb_array? ? h.merge(field => "#{unwound_jsonb_table_name}.#{column.split('::')[0]}") : h.merge(field => col)
61
63
  end
62
64
  end
63
65
 
@@ -108,4 +110,18 @@ class Hadouken::SqlBuilder
108
110
  column_name.scan(/[.]/).length <= 1 ? "\"#{column_name.split('.').join('"."')}\"" : "(#{column_name})"
109
111
  end
110
112
 
113
+ def scope_is_jsonb_array?
114
+ db_column = main_class.columns_hash[scope]
115
+ db_column&.type == :jsonb && JSON.parse(db_column.default.to_s).is_a?(Array)
116
+ end
117
+
118
+ def unwound_jsonb_table_name
119
+ @unwound_jsonb_table_name ||= ['array_from_', scope].join
120
+ end
121
+
122
+ def unwound_jsonb_table
123
+ table_structure = schema.values.map { |v| v.split('::').push('text')[0..1].join(' ') }.join(', ')
124
+ "jsonb_to_recordset(#{main_class.table_name}.#{scope}) AS #{unwound_jsonb_table_name}(#{table_structure})"
125
+ end
126
+
111
127
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hadouken-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Zaytsev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-09 00:00:00.000000000 Z
11
+ date: 2021-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
@@ -50,6 +50,7 @@ executables: []
50
50
  extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
+ - LICENSE
53
54
  - lib/hadouken-json.rb
54
55
  - lib/hadouken/decorator.rb
55
56
  - lib/hadouken/json.rb