cachengue 0.1.1

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: abdbe3f73ef48939bcd5a527588d3dabdcb9f743
4
+ data.tar.gz: 184300301128c201b944546a928fc9192c2ac052
5
+ SHA512:
6
+ metadata.gz: 2a312074235e514cfc7cf6275c2fe87adabd6cb1e545e5920d0337a2ebeb36b3211c6b4deb17b489841e7238750d43ab5aaded5424ba0e68f96c47edc1471991
7
+ data.tar.gz: 5d104102cddeca4a0075c0d9ecf8e568ac975f39aee63d879706f39dff34338b53c8038edaab32a2a3ffe1fb64bb268e2e0869e8ce18d9be5fef71afaaf3a150
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .DS_Store
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
@@ -0,0 +1,67 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/*
4
+ - config/**/*
5
+ - db/*
6
+ - db/migrate/*
7
+ - lib/tasks/*
8
+ - test/*
9
+ - Gemfile
10
+ - Rakefile
11
+ - config.ru
12
+ - lib/onfleet/onfleet-ruby/**/*
13
+
14
+ TargetRubyVersion: 2.4
15
+
16
+ Layout/IndentHash:
17
+ EnforcedStyle: consistent
18
+
19
+ Lint/MissingCopEnableDirective:
20
+ Enabled: false
21
+
22
+ Lint/SafeNavigationConsistency:
23
+ Enabled: false
24
+
25
+ Metrics/AbcSize:
26
+ Max: 50
27
+
28
+ Metrics/BlockLength:
29
+ Max: 50
30
+ Exclude:
31
+ - test/*/**
32
+
33
+ Metrics/ClassLength:
34
+ Max: 300
35
+ Exclude:
36
+ - test/*/**
37
+
38
+ Metrics/CyclomaticComplexity:
39
+ Max: 21
40
+
41
+ Metrics/PerceivedComplexity:
42
+ Max: 21
43
+
44
+ Metrics/LineLength:
45
+ Max: 90
46
+ Exclude:
47
+ - test/*/**
48
+
49
+ Metrics/MethodLength:
50
+ Max: 50
51
+
52
+ Metrics/ModuleLength:
53
+ Max: 300
54
+
55
+ Naming/MemoizedInstanceVariableName:
56
+ Enabled: false
57
+
58
+ Style/AsciiComments:
59
+ Enabled: false
60
+
61
+ Style/Documentation:
62
+ Enabled: false
63
+
64
+ Style/Lambda:
65
+ Enabled: false
66
+
67
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cachengue.gemspec
6
+ gemspec
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cachengue (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (5.1.6)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ concurrent-ruby (1.0.5)
15
+ i18n (1.0.1)
16
+ concurrent-ruby (~> 1.0)
17
+ minitest (5.3.5)
18
+ rake (10.5.0)
19
+ thread_safe (0.3.6)
20
+ tzinfo (1.2.5)
21
+ thread_safe (~> 0.1)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (~> 5.1.5)
28
+ bundler (~> 1.16)
29
+ cachengue!
30
+ minitest (~> 5.3.4)
31
+ rake (~> 10.0)
32
+
33
+ BUNDLED WITH
34
+ 1.16.2
@@ -0,0 +1,48 @@
1
+ # Cachengue
2
+
3
+ Welcome to cachengue gem!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cachengue'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cachengue
20
+
21
+ ## Usage
22
+
23
+
24
+ ###Examples:
25
+
26
+ In ```models/user.rb```
27
+ ```ruby
28
+ class User < ApplicationRecord
29
+ include Cachengue::ActiveRecord
30
+ end
31
+
32
+ User.cachengue.find_by(name: 'Jhon Doe')
33
+ ```
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitLab https://gitlab.com/shipnow/cachengue. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ # require "rspec/core/rake_task"
3
+ require 'rake/testtask'
4
+
5
+ # RSpec::Core::RakeTask.new(:spec)
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/**/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cachengue"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "cachengue/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "cachengue"
9
+ spec.version = Cachengue::VERSION
10
+ spec.licenses = ["MIT"]
11
+ spec.authors = ["shipnow developers"]
12
+ spec.email = ["developers@shipnow.com.ar"]
13
+
14
+ spec.summary = %q{cache gem for ruby on rails}
15
+ spec.description = %q{A gem for caching}
16
+ spec.homepage = "https://gitlab.com/shipnow/cachengue"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ # if spec.respond_to?(:metadata)
21
+ # spec.metadata["allowed_push_host"] = ""
22
+ # else
23
+ # raise "RubyGems 2.0 or newer is required to protect against " \
24
+ # "public gem pushes."
25
+ # end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ # spec.bindir = "exe"
33
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.3"
39
+ spec.add_development_dependency "activesupport", "~> 5.1"
40
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cachengue/version'
4
+ require 'cachengue/proxy'
5
+ require 'cachengue/caching'
6
+
7
+ module Cachengue
8
+ module ActiveRecord
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ extend Cachengue::Caching
13
+ before_save do
14
+ self.class.cachengue.cache_clear
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cachengue
4
+ module Caching
5
+ def cachengue(method_name = nil, &block)
6
+ return Cachengue::Proxy.new(self) if method_name.blank?
7
+
8
+ fetch(self, method_name, [], &block)
9
+ end
10
+
11
+ def module_cachengue(method_name, &block)
12
+ singleton_class.instance_eval do
13
+ define_method(method_name) do |*args|
14
+ Cachengue::Caching.fetch(self, method_name, args, &block)
15
+ end
16
+ end
17
+ end
18
+
19
+ module_function
20
+
21
+ def key_by(classx, method_name, args)
22
+ args = args.map(&:as_json)
23
+
24
+ "#{key_prefix(classx)}:#{method_name}:#{args}"
25
+ end
26
+
27
+ def key_prefix(classx)
28
+ "cache:#{classx.to_s.underscore}"
29
+ end
30
+
31
+ def keys(classx)
32
+ prefix = Cachengue::Caching.key_prefix(classx)
33
+
34
+ Rails.cache.data.keys("#{prefix}:*")
35
+ end
36
+
37
+ def clear(classx)
38
+ return if Rails.env.test?
39
+
40
+ keys(classx).map { |k| Rails.cache.delete(k) }.size
41
+ end
42
+
43
+ def fetch(classx, method_name, args)
44
+ key = Cachengue::Caching.key_by(classx, method_name, args)
45
+
46
+ new_block = proc do
47
+ value = yield(*args)
48
+
49
+ if defined?(::ActiveRecord) && value.is_a?(::ActiveRecord::Relation)
50
+ value.to_a
51
+ else
52
+ value
53
+ end
54
+ end
55
+
56
+ Rails.cache.fetch(key, &new_block)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Style/MethodMissingSuper
4
+
5
+ module Cachengue
6
+ class Proxy
7
+ def initialize(classx)
8
+ @classx = classx
9
+ end
10
+
11
+ def cache_keys
12
+ Cachengue::Caching.keys(@classx)
13
+ end
14
+
15
+ def cache_clear
16
+ Cachengue::Caching.clear(@classx)
17
+ end
18
+
19
+ private
20
+
21
+ def respond_to_missing?(method_name, include_all = false)
22
+ @classx.respond_to?(method_name, include_all) || super
23
+ end
24
+
25
+ def method_missing(method_name, *args)
26
+ Cachengue::Caching.fetch(@classx, method_name, args) do
27
+ @classx.public_send(method_name, *args)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cachengue
4
+ VERSION = '0.1.1'
5
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cachengue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - shipnow developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.1'
69
+ description: A gem for caching
70
+ email:
71
+ - developers@shipnow.com.ar
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rubocop.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - cachengue.gemspec
85
+ - lib/cachengue.rb
86
+ - lib/cachengue/caching.rb
87
+ - lib/cachengue/proxy.rb
88
+ - lib/cachengue/version.rb
89
+ homepage: https://gitlab.com/shipnow/cachengue
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.6.13
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: cache gem for ruby on rails
113
+ test_files: []