hash_map_attributes 0.1.0 → 0.1.1

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: 031aeb41226fbeb32e475cead38a155eb9c0d83281d88a6a377cf8d37aaf4423
4
- data.tar.gz: cb9af9bb97f00538649de4a1093cd1182298d3d81c92d94901b462355fbe0b9e
3
+ metadata.gz: 3803a8cf6fe2a0ff09709e71364e7b85da339a40e5192e9c618fd6265b9e27ad
4
+ data.tar.gz: f3c473e5f09e0052d0e5ddf91d6aacca5e528015c29bab621d15ce39d1e72e8e
5
5
  SHA512:
6
- metadata.gz: ab78ee7d3428f3d4e1dcb567109949b62c6bc26b2b408af7e7b6759907a516ab80fc68b5356f34a460cbf134963a5139b9aca90479d2fc4b0236d2f4202f913a
7
- data.tar.gz: e2b4956c01f1ac2d11a4122d48f0b9ea93c776c108362f64b2b334b60cc4042a6bacd18db70c41d7c181f817719091f93f1ba4e2d04ea8212c49c1d9c71e895d
6
+ metadata.gz: bf6326b47af7159a4cd86b8c0879a40157132ddab2b179da8eac6ba320cb1a2cea5efe3285dc84977a139b85a97b1a9704330615b85adef70305541ca3ee74ce
7
+ data.tar.gz: d4aa780a9c063961afd8d4e9518ba53c08a93c9869221f718851f0b0a65c7b9b23285d338ec126ac797b1f74ededaef107a9695428a6aa472c0615c1d7415c5c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_map_attributes (0.1.0)
4
+ hash_map_attributes (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- hash_map_attributes :image_url, :background_url, to: :extra_data
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', background_url: 'http://www.image.com/example2.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.background_url #=> http://www.image.com/example2.png
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', background_url: 'http://www.baklib.com')
49
+ Page.where_hash(image_url: 'http://www.baidu.com', content_background_url: 'http://www.baklib.com')
47
50
  ```
48
51
  控制台会打印如下查询语句
49
52
 
@@ -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[to] ||= Set.new
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.each_pair do |_to, _attributes|
17
- _attributes.each do |_attribute|
18
- define_method _attribute do
19
- send(_to).to_hash.stringify_keys![_attribute]
20
- end
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
- define_method "#{_attribute}=" do |v|
23
- send(_to).to_hash.stringify_keys![_attribute] = v
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
- to = _hash_map_attributes.select { |a, b| b.include?(k.to_s) }.keys.first
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.to_s}->>'#{k}' = '#{v}'"
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
@@ -1,3 +1,3 @@
1
1
  module HashMapAttributes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_map_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sumingxuan