attribute_cache 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0aa502ac547d83cddb4d74e57f1f845517c959983aa8adfb9e2fb381eeefeb87
4
+ data.tar.gz: 99699a40fe6da7cc3f83a16b8895d03ed571709ea347941e4906a0a303ebae99
5
+ SHA512:
6
+ metadata.gz: b3322e8c313a0895e2edccf339dc41473026debb37bb02b60c11a18fd701941a81f8de57ad3e4bbd914bda91e43ffbdcb76d8a0194725961293a5e670337b190
7
+ data.tar.gz: ec0fae3814b5ea459281d9fe7113ade28b416a0705fa53bafa16b0bca336b5db667d7465578ff8de026841cc1d5ed3b9b6e7b61662631cd5d982d96ad7c29c91
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Jacob Lockard
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.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # AttributeCache
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'attribute_cache'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install attribute_cache
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,47 @@
1
+ require 'attribute_cache/version'
2
+ require 'attribute_cache/railtie'
3
+
4
+ module AttributeCache
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def self.attribute_cache(name, attribute: nil, callback: nil, callback_prefix: 'calculate',
9
+ attribute_prefix: 'cached', &block)
10
+ attribute ||= :"#{attribute_prefix}_#{name}"
11
+ callback ||= :"#{callback_prefix}_#{name}"
12
+
13
+ set_method = "set_#{name}"
14
+ set_save_method = "set_#{name}!"
15
+
16
+ define_method set_method do
17
+ send("#{attribute}=", call_callback(callback, self, &block))
18
+ end
19
+ define_method("invalidate_#{name}") { send(set_method) }
20
+
21
+ define_method set_save_method do
22
+ send(set_method)
23
+ save
24
+ end
25
+ define_method("invalidate_#{name}!") { send(set_save_method) }
26
+
27
+ define_method name do |save: true, keep_blanks: true|
28
+ setter = save ? set_save_method : set_method
29
+ send(setter) if send(attribute).nil? || !keep_blanks && send(attribute).blank?
30
+ send(attribute)
31
+ end
32
+ end
33
+ end
34
+
35
+ def call_callback(callback, record)
36
+ return yield if block_given?
37
+
38
+ case callback
39
+ when Symbol, String
40
+ record.send(callback)
41
+ when Proc
42
+ callback[]
43
+ else
44
+ raise 'An invalid callback was provided!'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module AttributeCache
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module AttributeCache
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :attribute_cache do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Lockard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-18 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.1.2
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.2
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: byebug
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: factory_bot_rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 6.1.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 6.1.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec-rails
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 4.0.2
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 4.0.2
75
+ description: A simple Rails plugin that provides the ability to cache a value to a
76
+ model attribute. The attribute will be automatically set if necessary, retrieved
77
+ when requested, and can be manually invalidated.
78
+ email:
79
+ - jacoblockard99@hotmail.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - MIT-LICENSE
85
+ - README.md
86
+ - Rakefile
87
+ - lib/attribute_cache.rb
88
+ - lib/attribute_cache/railtie.rb
89
+ - lib/attribute_cache/version.rb
90
+ - lib/tasks/attribute_cache_tasks.rake
91
+ homepage: https://github.com/jacoblockard99/RailsAttributeCache.git
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ homepage_uri: https://github.com/jacoblockard99/RailsAttributeCache.git
96
+ source_code_uri: https://github.com/jacoblockard99/RailsAttributeCache.git
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.2.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A simple Rails plugin that provides the ability to cache a value to a model
116
+ attribute.
117
+ test_files: []