cache_ninja 0.1.0 → 1.0.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: de69c8a60362ab62ae38a34434db0dd99672d8b09db6c165d819ff86dbe472ae
4
- data.tar.gz: e7254abd7c50cac2b1733b5ecf6fec9e17fc83b8d8388b662195e0599243381b
3
+ metadata.gz: 46abc6d5a5ceda89bac2c41baafbed9b85c29a6afaeffec943979f2552eb45f3
4
+ data.tar.gz: e0e245bfa94e87ac59e2878ed21180658a4a1bf3fc34eaf63919427a49351cc6
5
5
  SHA512:
6
- metadata.gz: fb5c9f5639cfab13fe741eb4b55b8b031349440f1ac8381a987502fdc7e5f9fa29db3fd94cb56f2fc9b4ee6730b7c435cf6b13855ce88596d542551ea1877918
7
- data.tar.gz: f8e3539905667f9407b2d0e290cf9867029601060830bccb0f832c370b03cafc8b61aba24392511c57f1cd31c5205e1169fccbbe9300658200ce1df01dd6e5b2
6
+ metadata.gz: 5f7607a8f2f0497e8202e5263900c4f50f3776c01c2a7a906ae0052c31c9f9a2f9ead84f0fa5055eb716b5ced77b093dc9bb1fc8dcbe984fac8a9c4e77395d54
7
+ data.tar.gz: b3b76329bd4bd6abcc74005d1c5a89ff5f1088cc4499bb3d8f2a284a4ac90dafffb57cb0c6e679f4b78c794acd5a4584de6ec9c515e94ed347edd34f6552c8b5
data/cache_ninja.gemspec CHANGED
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
- spec.add_dependency 'rails', '~> 4.0'
35
+ spec.add_dependency 'rails', '>= 4.0'
36
36
 
37
37
  # For more information and examples about making a new gem, check out our
38
38
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -2,27 +2,26 @@ class FacelessObj
2
2
  attr_reader :attributes, :primary_key, :klass
3
3
 
4
4
  def initialize(object)
5
- @attributes = object.is_a?(Hash) ? object[:attributes] : object.attributes
6
- @primary_key = object.is_a?(Hash) ? object[:primary_key] : object.class.primary_key
7
- @klass = object.is_a?(Hash) ? object[:klass] : object.class
5
+ @attributes = extract_attributes(object)
6
+ @primary_key = extract_primary_key(object)
7
+ @klass = extract_class(object)
8
8
  create_getters
9
- create_assoc_data_getters
9
+ create_association_data_getters
10
10
  end
11
11
 
12
12
  def create_getters
13
13
  attributes.each do |key, value|
14
- self.define_singleton_method(key) do
15
- value
16
- end
14
+ define_singleton_method(key) { value }
17
15
  end
18
16
  end
19
17
 
20
- def create_assoc_data_getters
18
+ def create_association_data_getters
21
19
  return unless klass.respond_to?(:cached_associations)
20
+
22
21
  klass.cached_associations.each do |association|
23
- self.define_singleton_method("cached_#{association}") do
24
- data = klass.new(attributes).send("cached_#{association}")
25
- return data if data.present?
22
+ define_singleton_method("cached_#{association}") do
23
+ cached_data = klass.new(attributes).send("cached_#{association}")
24
+ return cached_data if cached_data.present?
26
25
  klass.new(attributes).send(association)
27
26
  end
28
27
  end
@@ -35,4 +34,18 @@ class FacelessObj
35
34
  klass: klass
36
35
  }
37
36
  end
37
+
38
+ private
39
+
40
+ def extract_attributes(object)
41
+ object.is_a?(Hash) ? object[:attributes] : object.attributes
42
+ end
43
+
44
+ def extract_primary_key(object)
45
+ object.is_a?(Hash) ? object[:primary_key] : object.class.primary_key
46
+ end
47
+
48
+ def extract_class(object)
49
+ object.is_a?(Hash) ? object[:klass] : object.class
50
+ end
38
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CacheNinja
4
- VERSION = "0.1.0"
4
+ VERSION = "1.0.1"
5
5
  end
data/lib/cache_ninja.rb CHANGED
@@ -1,24 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rails'
4
- require_relative "cache_ninja/version"
5
- require_relative "cache_ninja/faceless_obj"
4
+ require_relative 'cache_ninja/version'
5
+ require_relative 'cache_ninja/faceless_obj'
6
6
 
7
- module Cacheable
7
+ module CacheNinja
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  class_methods do
11
11
  def cached_associations
12
- @cached_associations || []
12
+ @cached_associations ||= []
13
13
  end
14
14
 
15
- def cache_assoc(associations, options={})
16
- disable = options[:disable]
17
- @cached_associations = associations.collect(&:to_sym)
18
- return if disable
19
- associations.each do |association|
20
- self.create_cached_association_method(association)
21
- end
15
+ def cache_assoc(associations, options = {})
16
+ return if options[:disable]
17
+
18
+ @cached_associations = associations.map(&:to_sym)
19
+ associations.each { |association| create_cached_association_method(association) }
22
20
  end
23
21
  end
24
22
 
@@ -29,22 +27,22 @@ module Cacheable
29
27
  define_method("cached_#{association}") do
30
28
  key = "#{self.class.name.downcase}_#{id}_cached_#{association}"
31
29
  cached_data = Rails.cache.fetch(key) do
32
- [send(association)].flatten.compact.collect { |obj| FacelessObj.new(obj).cacheable_data }
30
+ Array(send(association)).compact.map { |obj| FacelessObj.new(obj).cacheable_data }
33
31
  end
32
+
34
33
  singular_assoc = association.to_s.singularize
35
- data_objs = cached_data.collect { |obj| FacelessObj.new(obj) }
34
+ data_objs = cached_data.map { |obj| FacelessObj.new(obj) }
36
35
  return data_objs.first if association.to_s == singular_assoc
37
36
  data_objs
38
37
  end
39
38
  end
40
39
 
41
40
  def self.fetch_cached(id)
42
- cache_data = Rails.cache.fetch("#{self.to_s.underscore}/#{id}") do
43
- obj = self.find_by(id: id)
44
- if(obj)
45
- FacelessObj.new(obj).cacheable_data
46
- end
41
+ cache_data = Rails.cache.fetch("#{to_s.underscore}/#{id}") do
42
+ obj = find_by(id: id)
43
+ obj && FacelessObj.new(obj).cacheable_data
47
44
  end
45
+
48
46
  FacelessObj.new(cache_data) if cache_data.present?
49
47
  end
50
48
  end
@@ -56,10 +54,11 @@ module Cacheable
56
54
  self_n_parents = collect_parent_classes(self.class)
57
55
  self_n_parents.each do |self_n_parent|
58
56
  next unless self_n_parent.respond_to?(:cached_associations) && self_n_parent.cached_associations.present?
57
+
59
58
  self_n_parent.cached_associations.each do |association|
60
- # clear the cached associations
61
59
  Rails.cache.delete("#{self_n_parent.name.downcase}_#{id}_cached_#{association}")
62
- assoc_class_n_id = [self.send(association)].flatten.compact.collect { |obj| [obj.class, obj.id] }
60
+ assoc_class_n_id = Array(send(association)).compact.map { |obj| [obj.class, obj.id] }
61
+
63
62
  assoc_class_n_id.each do |assoc_class, assoc_id|
64
63
  assoc_self_n_parents = collect_parent_classes(assoc_class)
65
64
  assoc_self_n_parents.each do |assoc_self_n_parent|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_ninja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arjun Verma
@@ -14,14 +14,14 @@ dependencies:
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
27
  description: "**Cache Ninja** is the ultimate solution for safely and efficiently