codable 5.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
+ SHA1:
3
+ metadata.gz: 0a3b130028ee52ca86a896fcd2568a7560b8a29d
4
+ data.tar.gz: e3206fa1ecf7a2623df8a539b22512990730c4e6
5
+ SHA512:
6
+ metadata.gz: 42fc84008800d64f8aef58a45ff3412c404e338e6dae12d6c5ebd0e0bbc4e9bf87a2d15d89669823789dae1b98e4b5b523a27e59cef11221ab3ee0b8cb4c0a3e
7
+ data.tar.gz: e1df4235c569cbee2a1c93d04ef53eeb180a6ee4751eeaf7553f4e39547d0688390ec24f7b00f7edc14beef1eda029eda58bf61e861a6cfcfae51ba4f2de2cd2
@@ -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 = '5.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codable
3
+ version: !ruby/object:Gem::Version
4
+ version: 5.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: 5.x
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.x
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.3'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.5.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Rails plugin for the attribute - code
64
+ test_files: []