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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3803a8cf6fe2a0ff09709e71364e7b85da339a40e5192e9c618fd6265b9e27ad
4
- data.tar.gz: f3c473e5f09e0052d0e5ddf91d6aacca5e528015c29bab621d15ce39d1e72e8e
3
+ metadata.gz: 3bb07f14314b37ef85eea83ca583932197338e8561631c50c3d68d010340866e
4
+ data.tar.gz: e958087c61fcca8cbdf88761d63b16efc07fb3d002cae9d9587ae02e077b4755
5
5
  SHA512:
6
- metadata.gz: bf6326b47af7159a4cd86b8c0879a40157132ddab2b179da8eac6ba320cb1a2cea5efe3285dc84977a139b85a97b1a9704330615b85adef70305541ca3ee74ce
7
- data.tar.gz: d4aa780a9c063961afd8d4e9518ba53c08a93c9869221f718851f0b0a65c7b9b23285d338ec126ac797b1f74ededaef107a9695428a6aa472c0615c1d7415c5c
6
+ metadata.gz: 506095bca35ddeec0533f60ac9b4604289e466cedbcd3c8d2297fbe780085faade6db85a2a72446d9e2fd0606988af8a5185ac3e220385fc76adcf3f87ec4160
7
+ data.tar.gz: b691e5c62efb8f6f0a457693f7f9f4c820db0ea864e91026257606919ddb09db31504505d1f1d36340b34cc1f423dc1b8728c58e35f2eb86f11a18914e7b375f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_map_attributes (0.1.1)
4
+ hash_map_attributes (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 :image_url, to: :extra_data
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
- page = Page.new(image_url: 'http://www.image.com/example1.png', content_background_url: 'http://www.image.com/example2.png')
40
+ ```ruby
41
+ # 会根据 to 指定的字段添加对应的key
42
+ page = Page.create(left: nil)
41
43
  page.save
42
- page.image_url #=> http://www.image.com/example1.png
43
- page.content_background_url #=> http://www.image.com/example2.png
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(image_url: 'http://www.baidu.com', content_background_url: 'http://www.baklib.com')
76
+ Page.where_hash(left_cards_card1: 'left_cards_card1')
50
77
  ```
51
78
  控制台会打印如下查询语句
52
79
 
53
80
  ```sql
54
- SELECT "pages".* FROM "pages" WHERE (special_channels.extra_data->>'image_url' = 'http://www.baidu.com' and special_channels.extra_data->>'background_url' = 'http://www.baklib.com')
81
+ SELECT "special_channels".* FROM "special_channels" WHERE (special_channels.extra_data->'left'->'cards'->>'card1' = 'left_cards_card1')
55
82
  ```
@@ -6,38 +6,59 @@ module HashMapAttributes
6
6
  end
7
7
 
8
8
  class AttributeContainer
9
- attr_reader :attribute, :to, :prefix, :allow_nil, :method_name
10
- def initialize(attribute, to, prefix, allow_nil)
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
- @method_name = "#{@prefix}#{@attribute}"
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:, prefix: nil, allow_nil: nil)
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
- @_hash_map_attributes << AttributeContainer.new(attribute, to, prefix, allow_nil)
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
- send(_to).to_hash.stringify_keys![_attribute]
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
- send(_to).to_hash.stringify_keys![_attribute] = v
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
- next if to.nil?
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 << "#{class_name}.#{to}->>'#{_attribute}' = '#{v}'"
84
+ arr << "#{sql1}#{sql2}#{sql3}"
56
85
  end
57
86
  sql = arr.join(' and ')
58
87
  where(sql)
@@ -1,3 +1,3 @@
1
1
  module HashMapAttributes
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.1
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-04 00:00:00.000000000 Z
11
+ date: 2020-11-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: simple hash convert attributes
14
14
  email: