hash_map_attributes 0.1.1 → 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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +35 -8
- data/lib/hash_map_attributes.rb +44 -15
- data/lib/hash_map_attributes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb07f14314b37ef85eea83ca583932197338e8561631c50c3d68d010340866e
|
4
|
+
data.tar.gz: e958087c61fcca8cbdf88761d63b16efc07fb3d002cae9d9587ae02e077b4755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 506095bca35ddeec0533f60ac9b4604289e466cedbcd3c8d2297fbe780085faade6db85a2a72446d9e2fd0606988af8a5185ac3e220385fc76adcf3f87ec4160
|
7
|
+
data.tar.gz: b691e5c62efb8f6f0a457693f7f9f4c820db0ea864e91026257606919ddb09db31504505d1f1d36340b34cc1f423dc1b8728c58e35f2eb86f11a18914e7b375f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -32,24 +32,51 @@ class Page < ApplicationRecord
|
|
32
32
|
# created_at :datetime not null
|
33
33
|
# updated_at :datetime not null
|
34
34
|
include HashMapAttributes
|
35
|
-
hash_map_attributes :
|
36
|
-
hash_map_attributes :background_url, prefix: :content, to: :extra_data
|
37
|
-
|
35
|
+
hash_map_attributes :left, :right, to: :extra_data
|
38
36
|
end
|
37
|
+
```
|
38
|
+
### 创建或修改
|
39
39
|
|
40
|
-
|
40
|
+
```ruby
|
41
|
+
# 会根据 to 指定的字段添加对应的key
|
42
|
+
page = Page.create(left: nil)
|
41
43
|
page.save
|
42
|
-
page.
|
43
|
-
|
44
|
+
page.extra_data #=> {"left"=>nil}
|
45
|
+
|
44
46
|
```
|
47
|
+
|
48
|
+
如果我们需要嵌套 hash, 需要使用 parent 参数指定上一层的键
|
49
|
+
```ruby
|
50
|
+
class Page < ApplicationRecord
|
51
|
+
hash_map_attributes :logo, :title :cards, parent: :left
|
52
|
+
hash_map_attributes :logo, :title :cards, parent: :right
|
53
|
+
end
|
54
|
+
|
55
|
+
# 也可以使用 update 去更新对应区域的值
|
56
|
+
page.right_logo = 'right_logo1'
|
57
|
+
page.extra_data #=> {"left"=>nil, "right"=>{"logo"=>"right_logo1"}}
|
58
|
+
```
|
59
|
+
|
60
|
+
多层嵌套
|
61
|
+
```ruby
|
62
|
+
class Page < ApplicationRecord
|
63
|
+
# parent 指定的是上一层的自动连接起来的命名
|
64
|
+
hash_map_attributes :card1, :card2, parent: :left_cards
|
65
|
+
hash_map_attributes :card1, :card2, parent: :right_cards
|
66
|
+
end
|
67
|
+
|
68
|
+
page.left_cards_card1 = 'left_cards_card1'
|
69
|
+
page.extra_data #=> {"left"=>{"cards"=>{"card1"=>"left_cards_card1"}},"right"=>{"logo"=>"right_logo1"}}
|
70
|
+
```
|
71
|
+
|
45
72
|
### 查询
|
46
73
|
|
47
74
|
如果是 jsonb 的话,那么则支持查询
|
48
75
|
```ruby
|
49
|
-
Page.where_hash(
|
76
|
+
Page.where_hash(left_cards_card1: 'left_cards_card1')
|
50
77
|
```
|
51
78
|
控制台会打印如下查询语句
|
52
79
|
|
53
80
|
```sql
|
54
|
-
SELECT "
|
81
|
+
SELECT "special_channels".* FROM "special_channels" WHERE (special_channels.extra_data->'left'->'cards'->>'card1' = 'left_cards_card1')
|
55
82
|
```
|
data/lib/hash_map_attributes.rb
CHANGED
@@ -6,38 +6,59 @@ module HashMapAttributes
|
|
6
6
|
end
|
7
7
|
|
8
8
|
class AttributeContainer
|
9
|
-
attr_reader :attribute, :to, :
|
10
|
-
def initialize(attribute, to,
|
9
|
+
attr_reader :attribute, :to, :allow_nil, :method_name, :ancestors_name, :parent
|
10
|
+
def initialize(attribute, to, allow_nil, parent)
|
11
11
|
@to = to.to_s
|
12
|
-
@prefix = \
|
13
|
-
if prefix
|
14
|
-
"#{prefix == true ? to : prefix}_"
|
15
|
-
else
|
16
|
-
''
|
17
|
-
end
|
18
12
|
@attribute = attribute.to_s
|
19
13
|
@allow_nil = allow_nil
|
20
|
-
@
|
14
|
+
@parent = parent
|
15
|
+
@method_name = @attribute
|
16
|
+
@ancestors_name = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_own_ancestors!(all_ancestores)
|
20
|
+
parent_container = all_ancestores.find { |a| a.method_name == parent.to_s }
|
21
|
+
@ancestors_name = parent_container.ancestors_name + [parent_container.attribute]
|
22
|
+
@to = parent_container.to
|
23
|
+
@method_name = \
|
24
|
+
if @ancestors_name.blank?
|
25
|
+
@attribute
|
26
|
+
else
|
27
|
+
"#{@ancestors_name.join('_')}_#{@attribute}"
|
28
|
+
end
|
21
29
|
end
|
22
30
|
end
|
23
31
|
|
24
32
|
module ClassMethods
|
25
|
-
def hash_map_attributes(*attributes, to
|
33
|
+
def hash_map_attributes(*attributes, to: nil, allow_nil: nil, parent: nil)
|
26
34
|
_hash_map_attributes
|
27
35
|
attributes.each do |attribute|
|
28
|
-
|
36
|
+
container = AttributeContainer.new(attribute, to, allow_nil, parent)
|
37
|
+
container.set_own_ancestors!(@_hash_map_attributes) if parent
|
38
|
+
@_hash_map_attributes << container
|
29
39
|
end
|
30
40
|
class_eval do
|
31
41
|
_hash_map_attributes.each do |container|
|
32
42
|
_attribute = container.attribute
|
33
43
|
_to = container.to
|
44
|
+
_parent = container.parent
|
34
45
|
method_name = container.method_name
|
35
46
|
define_method method_name do
|
36
|
-
|
47
|
+
if _parent
|
48
|
+
send(_parent).to_hash.stringify_keys![_attribute] rescue nil
|
49
|
+
else
|
50
|
+
send(_to).to_hash.stringify_keys![_attribute] rescue nil
|
51
|
+
end
|
37
52
|
end
|
38
53
|
|
39
54
|
define_method "#{method_name}=" do |v|
|
40
|
-
|
55
|
+
if _parent
|
56
|
+
send("#{_parent}=", {}) if send(_parent).blank?
|
57
|
+
send(_parent).to_hash.stringify_keys![_attribute] = v
|
58
|
+
else
|
59
|
+
send("#{_to}=", {}) if send(_to).blank?
|
60
|
+
send(_to).to_hash.stringify_keys![_attribute] = v
|
61
|
+
end
|
41
62
|
end
|
42
63
|
end
|
43
64
|
end
|
@@ -48,11 +69,19 @@ module HashMapAttributes
|
|
48
69
|
class_name = model_name.plural
|
49
70
|
options.each_pair do |k, v|
|
50
71
|
container = _hash_map_attributes.find { |a| a.method_name == k.to_s }
|
72
|
+
next if container.nil?
|
73
|
+
|
51
74
|
to = container.to.to_s
|
52
75
|
_attribute = container.attribute.to_s
|
53
|
-
|
76
|
+
_ancestors_name = container.ancestors_name
|
77
|
+
sql1 = "#{class_name}.#{to}"
|
78
|
+
sql2 = \
|
79
|
+
unless _ancestors_name.blank?
|
80
|
+
%Q|->#{_ancestors_name.map {|a| "'#{a}'"}.join('->')}|
|
81
|
+
end
|
82
|
+
sql3 = "->>'#{_attribute}' = '#{v}'"
|
54
83
|
|
55
|
-
arr << "#{
|
84
|
+
arr << "#{sql1}#{sql2}#{sql3}"
|
56
85
|
end
|
57
86
|
sql = arr.join(' and ')
|
58
87
|
where(sql)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_map_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sumingxuan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: simple hash convert attributes
|
14
14
|
email:
|