hash_map_attributes 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 031aeb41226fbeb32e475cead38a155eb9c0d83281d88a6a377cf8d37aaf4423
4
+ data.tar.gz: cb9af9bb97f00538649de4a1093cd1182298d3d81c92d94901b462355fbe0b9e
5
+ SHA512:
6
+ metadata.gz: ab78ee7d3428f3d4e1dcb567109949b62c6bc26b2b408af7e7b6759907a516ab80fc68b5356f34a460cbf134963a5139b9aca90479d2fc4b0236d2f4202f913a
7
+ data.tar.gz: e2b4956c01f1ac2d11a4122d48f0b9ea93c776c108362f64b2b334b60cc4042a6bacd18db70c41d7c181f817719091f93f1ba4e2d04ea8212c49c1d9c71e895d
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hash_map_attributes.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hash_map_attributes (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (12.3.3)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ hash_map_attributes!
16
+ rake (~> 12.0)
17
+
18
+ BUNDLED WITH
19
+ 2.1.4
@@ -0,0 +1,52 @@
1
+ # HashMapAttributes
2
+
3
+ 将你json类型的字段映射为字段去操作
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hash_map_attributes'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hash_map_attributes
20
+
21
+ ## Usage
22
+ ```ruby
23
+ class Page < ApplicationRecord
24
+ # == Schema Information
25
+ #
26
+ # Table name: special_channels
27
+ #
28
+ # id :bigint not null, primary key
29
+ # description :string
30
+ # title :string
31
+ # extra_data :jsonb
32
+ # created_at :datetime not null
33
+ # updated_at :datetime not null
34
+ hash_map_attributes :image_url, :background_url, to: :extra_data
35
+ end
36
+
37
+ page = Page.new(image_url: 'http://www.image.com/example1.png', background_url: 'http://www.image.com/example2.png')
38
+ page.save
39
+ page.image_url #=> http://www.image.com/example1.png
40
+ page.background_url #=> http://www.image.com/example2.png
41
+
42
+ ```
43
+ ### 查询
44
+
45
+ ```ruby
46
+ Page.where_hash(image_url: 'http://www.baidu.com', background_url: 'http://www.baklib.com')
47
+ ```
48
+ 控制台会打印如下查询语句
49
+
50
+ ```sql
51
+ 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')
52
+ ```
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ require_relative 'lib/hash_map_attributes/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "hash_map_attributes"
5
+ spec.version = HashMapAttributes::VERSION
6
+ spec.authors = ["sumingxuan"]
7
+ spec.email = ["1154621382@qq.com"]
8
+
9
+ spec.summary = %q{simple hash convert attributes}
10
+ spec.description = %q{simple hash convert attributes}
11
+ spec.homepage = "https://github.com/SuMingXuan/hash_map_attributes"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,47 @@
1
+ require "hash_map_attributes/version"
2
+
3
+ module HashMapAttributes
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def hash_map_attributes(*attributes, to:)
10
+ _hash_map_attributes
11
+ attributes.each do |attribute|
12
+ @_hash_map_attributes[to] ||= Set.new
13
+ @_hash_map_attributes[to] << attribute.to_s
14
+ end
15
+ 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
21
+
22
+ define_method "#{_attribute}=" do |v|
23
+ send(_to).to_hash.stringify_keys![_attribute] = v
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def where_hash(**options)
31
+ arr = []
32
+ class_name = model_name.plural
33
+ options.each_pair do |k, v|
34
+ to = _hash_map_attributes.select { |a, b| b.include?(k.to_s) }.keys.first
35
+ next if to.nil?
36
+
37
+ arr << "#{class_name}.#{to.to_s}->>'#{k}' = '#{v}'"
38
+ end
39
+ sql = arr.join(' and ')
40
+ where(sql)
41
+ end
42
+
43
+ def _hash_map_attributes
44
+ @_hash_map_attributes ||= {}
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module HashMapAttributes
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_map_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sumingxuan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: simple hash convert attributes
14
+ email:
15
+ - 1154621382@qq.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - README.md
24
+ - Rakefile
25
+ - hash_map_attributes.gemspec
26
+ - lib/hash_map_attributes.rb
27
+ - lib/hash_map_attributes/version.rb
28
+ homepage: https://github.com/SuMingXuan/hash_map_attributes
29
+ licenses: []
30
+ metadata:
31
+ homepage_uri: https://github.com/SuMingXuan/hash_map_attributes
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.3.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.0.8
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: simple hash convert attributes
51
+ test_files: []