artirix_cache_service 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8925a97c03f8fda41870180966bfef9e8b224a3
4
+ data.tar.gz: 453fb7acd85987d1ee0ed7728718a91dc00d68c8
5
+ SHA512:
6
+ metadata.gz: 1dd2bae6ef68232ae2971aa016f3a04173ff4a03fc9d41598ffc07d10a91eeefde48be2fafe1357f03e564bdeee62c28d45e92bf2ee4f551a06f359d3a443721
7
+ data.tar.gz: 443f473eccdbf0908a9efdf11e05e046abc052ac8ebe6faf6811cc75717bed1c23c3f13280a6b22929da0c89c20766cf9411e48bb666328b0fd090d35da24064
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.idea
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.3
5
+
6
+ addons:
7
+ code_climate:
8
+ repo_token: 39d61ce8353fb7524448b8deb2b5a4c0147dd787ed4a47dd0c6e133d812139b4
9
+
10
+ script: 'bundle exec rake spec'
11
+
12
+ notifications:
13
+ email:
14
+ recipients:
15
+ - eturino@eturino.com
16
+ on_failure: change
17
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html_surgeon.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry'
8
+ gem 'pry-nav'
9
+ gem 'pry-stack_explorer'
10
+ gem 'pry-doc'
11
+ gem 'pry-rescue'
12
+ end
13
+
14
+ gem 'codeclimate-test-reporter', :group => :test, :require => nil
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # ArtirixCacheService
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/artirix_cache_service.svg)](http://badge.fury.io/rb/artirix_cache_service)
4
+ [![Build Status](https://travis-ci.org/artirix/artirix_cache_service.svg?branch=master)](https://travis-ci.org/artirix/artirix_cache_service)
5
+ [![Code Climate](https://codeclimate.com/github/artirix/artirix_cache_service.png)](https://codeclimate.com/github/artirix/artirix_cache_service)
6
+ [![Code Climate Coverage](https://codeclimate.com/github/artirix/artirix_cache_service/coverage.png)](https://codeclimate.com/github/artirix/artirix_cache_service)
7
+
8
+ The basic use of this gem is to compile a cache key based on a given key prefix
9
+ and some extra variables or options, with some helper methods.
10
+
11
+ TODO: also help with the cache options.
12
+
13
+ ## Usage
14
+
15
+ The basic way of using it is with the `key` method, which will return the key based on the given arguments.
16
+
17
+ ```ruby
18
+ ArtirixCacheService.key :some_key # => will return a string with the cache key to use
19
+ ```
20
+
21
+ ### Prefix
22
+
23
+ The service can use a prefix to be applied to all keys
24
+
25
+ ```ruby
26
+ ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
27
+ ArtirixCacheService.key :some_key # => "configured_prefix/some_key"
28
+ ArtirixCacheService.key :another # => "configured_prefix/another"
29
+ ```
30
+
31
+ ### Extra Arguments
32
+
33
+ We can pass other arguments, that will be treated and appended to the cache key.
34
+
35
+ note: `blank?` arguments will be skipped.
36
+
37
+ ```ruby
38
+ ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
39
+
40
+ ArtirixCacheService.key :some_key, :arg1, nil, 'arg2'
41
+ # => "configured_prefix/some_key/arg1/arg2"
42
+ ```
43
+
44
+ #### `cache_key` compliant arguments
45
+
46
+ if an argument (including the first argument) responds to `cache_key`,
47
+ it will be called.
48
+
49
+ ```ruby
50
+ ArtirixCacheService.config_params[:key_prefix] = :configured_prefix
51
+
52
+ article = Article.find 17
53
+ article.cache_key # => "cache_key_article_17"
54
+
55
+ ArtirixCacheService.key :some_key, :arg1, article, 'arg2'
56
+ # => "configured_prefix/some_key/arg1/cache_key_article_17/arg2"
57
+ ```
58
+
59
+ #### Digest
60
+
61
+ we may want to add a digest to the cache key instead of all arguments,
62
+ for example in case that we're giving it a long list.
63
+
64
+ It will use SHA1.
65
+
66
+ ```ruby
67
+ ArtirixCacheService.config_params[:key_prefix] = :prfx
68
+
69
+ arg3 = { a: 1, b: 2 }
70
+ ArtirixCacheService.digest arg3
71
+ # => "032b5f154d4ada01bc89a2e8fae8251c090212db"
72
+
73
+ ArtirixCacheService.key :some_key, :arg1, 'arg2', digest: arg3
74
+ # => "prfx/some_key/arg1/arg2/032b5f154d4ada01bc89a2e8fae8251c090212db"
75
+
76
+ arg4 = [1, 2, 3]
77
+ ArtirixCacheService.digest [arg3, arg4]
78
+ # => "7448a071aeee91fc9ee1c705f15445fdd8411224"
79
+
80
+
81
+ ArtirixCacheService.key :some_key, :arg1, 'arg2', digest: [arg3, arg4]
82
+ # => "prfx/some_key/arg1/arg2/7448a071aeee91fc9ee1c705f15445fdd8411224"
83
+ ```
84
+
85
+
86
+ ## Installation
87
+
88
+ Add this line to your application's Gemfile:
89
+
90
+ ```ruby
91
+ gem 'artirix_cache_service'
92
+ ```
93
+
94
+ And then execute:
95
+
96
+ $ bundle
97
+
98
+ Or install it yourself as:
99
+
100
+ $ gem install artirix_cache_service
101
+
102
+ ## Development
103
+
104
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
105
+
106
+ 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).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/artirix/artirix_cache_service.
111
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'artirix_cache_service/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'artirix_cache_service'
8
+ spec.version = ArtirixCacheService::VERSION
9
+ spec.authors = ['Eduardo Turiño']
10
+ spec.email = ['eturino@eturino.com']
11
+
12
+ spec.summary = %q{Common Cache Helpers for rails apps}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = 'https://github.com/artirix/artirix_cache_service'
15
+
16
+ # # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activesupport', '~> 4'
30
+ spec.add_development_dependency 'bundler', '~> 1.10'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec'
33
+ spec.add_development_dependency 'faker'
34
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "artirix_cache_service"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ module ArtirixCacheService
2
+ class Key
3
+ KEY_SEPARATOR = '/'.freeze
4
+
5
+ attr_reader :args, :service
6
+
7
+ def initialize(given_args, service)
8
+ @service = service
9
+ @args = clean_key_args given_args
10
+ end
11
+
12
+ delegate :key_prefix, :digest, to: :service
13
+
14
+ def call
15
+ clean_parts.join KEY_SEPARATOR
16
+ end
17
+
18
+ private
19
+
20
+ def clean_parts
21
+ parts.map(&:presence).compact.map(&:to_s)
22
+ end
23
+
24
+ def parts
25
+ [key_prefix].concat(args)
26
+ end
27
+
28
+ def clean_key_args(args)
29
+ args.map { |a| clean_arg a }.flatten
30
+ end
31
+
32
+ def clean_arg(arg)
33
+ cache_key_from_model(arg) || cache_key_from_options(arg) || arg
34
+ end
35
+
36
+ def cache_key_from_model(model)
37
+ model.try(:cache_key)
38
+ end
39
+
40
+ def cache_key_from_options(hash)
41
+ return nil unless hash.kind_of? Hash
42
+ return nil unless hash[:digest]
43
+
44
+ digest hash[:digest]
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,20 @@
1
+ module ArtirixCacheService
2
+ class Service
3
+ # Instance
4
+ def config_params
5
+ @config_params ||= { key_prefix: nil }
6
+ end
7
+
8
+ def key_prefix
9
+ config_params[:key_prefix]
10
+ end
11
+
12
+ def key(*given_args)
13
+ Key.new(given_args, self).call
14
+ end
15
+
16
+ def digest(arg)
17
+ Digest::SHA1.hexdigest arg.to_s
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module ArtirixCacheService
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/all'
2
+ require 'artirix_cache_service/version'
3
+ require 'artirix_cache_service/key'
4
+ require 'artirix_cache_service/service'
5
+
6
+ module ArtirixCacheService
7
+
8
+ # Delegation of static methods to the Service instance
9
+ class << self
10
+ delegate :key, :config_params, :key_prefix, :digest, to: :service
11
+ end
12
+
13
+ def self.service
14
+ @service ||= reload_service
15
+ end
16
+
17
+ def self.reload_service
18
+ @service = Service.new
19
+ end
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artirix_cache_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo Turiño
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - eturino@eturino.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - artirix_cache_service.gemspec
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/artirix_cache_service.rb
100
+ - lib/artirix_cache_service/key.rb
101
+ - lib/artirix_cache_service/service.rb
102
+ - lib/artirix_cache_service/version.rb
103
+ homepage: https://github.com/artirix/artirix_cache_service
104
+ licenses: []
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.6
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Common Cache Helpers for rails apps
126
+ test_files: []
127
+ has_rdoc: