hash_map_attributes 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -5
- data/lib/hash_map_attributes.rb +32 -14
- data/lib/hash_map_attributes/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3803a8cf6fe2a0ff09709e71364e7b85da339a40e5192e9c618fd6265b9e27ad
|
4
|
+
data.tar.gz: f3c473e5f09e0052d0e5ddf91d6aacca5e528015c29bab621d15ce39d1e72e8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf6326b47af7159a4cd86b8c0879a40157132ddab2b179da8eac6ba320cb1a2cea5efe3285dc84977a139b85a97b1a9704330615b85adef70305541ca3ee74ce
|
7
|
+
data.tar.gz: d4aa780a9c063961afd8d4e9518ba53c08a93c9869221f718851f0b0a65c7b9b23285d338ec126ac797b1f74ededaef107a9695428a6aa472c0615c1d7415c5c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -31,19 +31,22 @@ class Page < ApplicationRecord
|
|
31
31
|
# extra_data :jsonb
|
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
38
|
end
|
36
39
|
|
37
|
-
page = Page.new(image_url: 'http://www.image.com/example1.png',
|
40
|
+
page = Page.new(image_url: 'http://www.image.com/example1.png', content_background_url: 'http://www.image.com/example2.png')
|
38
41
|
page.save
|
39
42
|
page.image_url #=> http://www.image.com/example1.png
|
40
|
-
page.
|
41
|
-
|
43
|
+
page.content_background_url #=> http://www.image.com/example2.png
|
42
44
|
```
|
43
45
|
### 查询
|
44
46
|
|
47
|
+
如果是 jsonb 的话,那么则支持查询
|
45
48
|
```ruby
|
46
|
-
Page.where_hash(image_url: 'http://www.baidu.com',
|
49
|
+
Page.where_hash(image_url: 'http://www.baidu.com', content_background_url: 'http://www.baklib.com')
|
47
50
|
```
|
48
51
|
控制台会打印如下查询语句
|
49
52
|
|
data/lib/hash_map_attributes.rb
CHANGED
@@ -5,23 +5,39 @@ module HashMapAttributes
|
|
5
5
|
base.extend ClassMethods
|
6
6
|
end
|
7
7
|
|
8
|
+
class AttributeContainer
|
9
|
+
attr_reader :attribute, :to, :prefix, :allow_nil, :method_name
|
10
|
+
def initialize(attribute, to, prefix, allow_nil)
|
11
|
+
@to = to.to_s
|
12
|
+
@prefix = \
|
13
|
+
if prefix
|
14
|
+
"#{prefix == true ? to : prefix}_"
|
15
|
+
else
|
16
|
+
''
|
17
|
+
end
|
18
|
+
@attribute = attribute.to_s
|
19
|
+
@allow_nil = allow_nil
|
20
|
+
@method_name = "#{@prefix}#{@attribute}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
8
24
|
module ClassMethods
|
9
|
-
def hash_map_attributes(*attributes, to:)
|
25
|
+
def hash_map_attributes(*attributes, to:, prefix: nil, allow_nil: nil)
|
10
26
|
_hash_map_attributes
|
11
27
|
attributes.each do |attribute|
|
12
|
-
@_hash_map_attributes
|
13
|
-
@_hash_map_attributes[to] << attribute.to_s
|
28
|
+
@_hash_map_attributes << AttributeContainer.new(attribute, to, prefix, allow_nil)
|
14
29
|
end
|
15
30
|
class_eval do
|
16
|
-
_hash_map_attributes.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
31
|
+
_hash_map_attributes.each do |container|
|
32
|
+
_attribute = container.attribute
|
33
|
+
_to = container.to
|
34
|
+
method_name = container.method_name
|
35
|
+
define_method method_name do
|
36
|
+
send(_to).to_hash.stringify_keys![_attribute]
|
37
|
+
end
|
21
38
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
39
|
+
define_method "#{method_name}=" do |v|
|
40
|
+
send(_to).to_hash.stringify_keys![_attribute] = v
|
25
41
|
end
|
26
42
|
end
|
27
43
|
end
|
@@ -31,17 +47,19 @@ module HashMapAttributes
|
|
31
47
|
arr = []
|
32
48
|
class_name = model_name.plural
|
33
49
|
options.each_pair do |k, v|
|
34
|
-
|
50
|
+
container = _hash_map_attributes.find { |a| a.method_name == k.to_s }
|
51
|
+
to = container.to.to_s
|
52
|
+
_attribute = container.attribute.to_s
|
35
53
|
next if to.nil?
|
36
54
|
|
37
|
-
arr << "#{class_name}.#{to
|
55
|
+
arr << "#{class_name}.#{to}->>'#{_attribute}' = '#{v}'"
|
38
56
|
end
|
39
57
|
sql = arr.join(' and ')
|
40
58
|
where(sql)
|
41
59
|
end
|
42
60
|
|
43
61
|
def _hash_map_attributes
|
44
|
-
@_hash_map_attributes ||=
|
62
|
+
@_hash_map_attributes ||= []
|
45
63
|
end
|
46
64
|
end
|
47
65
|
end
|