codable 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c30b4e9606bd7846f4827b2f16f918e04d03772770b6c71aabfdd6270ef73568
4
+ data.tar.gz: 0b42b02efb7900a0bcd2c722e480b858f8f862619a026d999540de47ffddb915
5
+ SHA512:
6
+ metadata.gz: '07418da06c7b74d3ef1126fcdb5ae4cab5951e4c82397b40a7907a1ebcc581ea93e9c0bae31375ddd4114a8d7498cfef013d68fd165c575d03c02c45ca537ae4'
7
+ data.tar.gz: 712d4af049d0d458e91a54bffa6d48acb2e99b4353b6629220463c25e90de81236aeb782435be886229fbb53f027083610ffe8a29fb1297f6803e2a34afc6a4f
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Song Huang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Codable
2
+ Rails plugin for the attribute - code
3
+
4
+ ## Installation
5
+ Include the gem to your Gemfile:
6
+ ```ruby
7
+ gem 'codable', '~> 6.x' # For Rails 6.x
8
+ ```
9
+
10
+ ## Usage
11
+ Include Codable for your model:
12
+ ```ruby
13
+ class Platform < ApplicationRecord
14
+ include Codable
15
+ self.codable_key = :other # You needn't do this until the attribute is not `code`
16
+ end
17
+ ```
18
+
19
+ ```ruby
20
+ Platform.create!([
21
+ { id: 1, code: 'linux' },
22
+ { id: 2, code: 'macos' },
23
+ ])
24
+ Platform[:linux] # => #<Platform: 1>
25
+ Platform[:linux].id # => 1, hit cache this time
26
+ Platform[:linux].linux? # => true, hit cache this time
27
+ Platform[:linux].macos? # => false, hit cache this time
28
+ ```
29
+
30
+ ## License
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Codable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,49 @@
1
+ require 'codable/railtie'
2
+
3
+ # Rails plugin for the attribute - code
4
+ module Codable
5
+ extend ActiveSupport::Concern
6
+
7
+ class_methods do
8
+ attr_accessor :codable_key
9
+
10
+ def [](value)
11
+ raise ArgumentError, 'value must be present' unless value.present?
12
+
13
+ Rails.cache.fetch("#{_codable_cache_key_prefix}/#{value}") do
14
+ find_by!(codable_key => value)
15
+ end
16
+ end
17
+
18
+ def _codable_cache_key_prefix
19
+ "codable/#{table_name}/#{codable_key}"
20
+ end
21
+ end
22
+
23
+ included do
24
+ raise ArgumentError, 'Class included must be a ActiveRecord' unless is_a?(Class) && self <= ActiveRecord::Base
25
+
26
+ after_commit :_codable_clear_expired_cache, on: %i[update destroy]
27
+ self.codable_key = :code
28
+ end
29
+
30
+ def method_missing(method, *args)
31
+ if method.to_s.end_with?('?')
32
+ attributes[self.class.codable_key.to_s] == method.to_s.chomp('?')
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ def respond_to_missing?(method, include_private = false)
39
+ method.to_s.end_with?('?') || super
40
+ end
41
+
42
+ private
43
+
44
+ def _codable_clear_expired_cache
45
+ # When the codable_key field is modified, we need to remove the cache of the original value
46
+ value = attribute_before_last_save(self.class.codable_key.to_s) || attributes[self.class.codable_key.to_s]
47
+ Rails.cache.delete("#{self.class._codable_cache_key_prefix}/#{value}")
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ module Codable
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Codable
2
+ VERSION = '6.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codable
3
+ version: !ruby/object:Gem::Version
4
+ version: 6.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Song Huang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: Rails plugin for the attribute - code
28
+ email:
29
+ - songhuangcn@qq.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/codable.rb
38
+ - lib/codable/railtie.rb
39
+ - lib/codable/version.rb
40
+ homepage: https://github.com/songhuangcn/codable
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '2.5'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.2
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Rails plugin for the attribute - code
63
+ test_files: []