cachew 0.1.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: bee898b623cf6399bb9a8b2d281400561ef965ba
4
+ data.tar.gz: 2630810c58528c2e8c0f430740cf1bc16cd76bc3
5
+ SHA512:
6
+ metadata.gz: 372ff7e2c634a068d861d33b22b45b57afd0f3ffa14f35c9e9867d33f904049214ce058e3c745f7dd8585a62d4712b5f0ffb0e5f564f554bdfbb2ee2272363ab
7
+ data.tar.gz: b64586d8a75d0e07b784cf5870c3d5421a055b0ad567150d6449905b98610a49cbb41ae33c082a86e1f80906d8b0fe4256838ae66433bbf947a2348568b24413
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,36 @@
1
+ # Avoid long parameter lists
2
+ ParameterLists:
3
+ Max: 3
4
+ CountKeywordArgs: true
5
+
6
+ MethodLength:
7
+ CountComments: false
8
+ Max: 15
9
+
10
+ ClassLength:
11
+ CountComments: false
12
+ Max: 100
13
+
14
+ CyclomaticComplexity:
15
+ Max: 6
16
+
17
+ BlockNesting:
18
+ Max: 3
19
+
20
+ HashSyntax:
21
+ EnforcedStyle: hash_rockets
22
+
23
+ Encoding:
24
+ Enabled: false
25
+
26
+ StringLiterals:
27
+ EnforcedStyle: double_quotes
28
+
29
+ BracesAroundHashParameters:
30
+ Enabled: false
31
+
32
+ Documentation:
33
+ Enabled: false
34
+
35
+ EmptyLines:
36
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rake"
4
+ gem "rspec"
5
+ gem "guard-rspec"
6
+ gem "rubocop"
7
+ gem "coveralls", :require => false
8
+ gem "simplecov", :require => false
9
+
10
+ platforms :rbx do
11
+ gem "racc"
12
+ gem "rubinius-coverage", "~> 2.0"
13
+ gem "rubysl", "~> 2.0"
14
+ end
15
+
16
+ # Specify your gem's dependencies in cachew.gemspec
17
+ gemspec
@@ -0,0 +1,6 @@
1
+ guard :rspec do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Aleksey V Zapparov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Cachew
2
+
3
+ Simple (and unified) cache interface.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cachew'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cachew
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ # With HashAdapter
23
+ cache = Cachew.new({})
24
+
25
+ cache.has?(:foo) # => false
26
+ cache.fetch(:foo) { :bar } # => :bar
27
+
28
+ cache.has?(:foo) # => true
29
+ cache.fetch(:foo) { :moo } # => :bar
30
+
31
+
32
+ # With (default) NullAdapter
33
+ cache = Cachew.new
34
+
35
+ cache.has?(:foo) # => false
36
+ cache.fetch(:foo) { :bar } # => :bar
37
+
38
+ cache.has?(:foo) # => false
39
+ cache.fetch(:foo) { :moo } # => :moo
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( http://github.com/<my-github-username>/cachew/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new
5
+
6
+ require 'rubocop/rake_task'
7
+ Rubocop::RakeTask.new
8
+
9
+ task :default => [:spec, :rubocop]
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cachew/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cachew"
8
+ spec.version = Cachew::VERSION
9
+ spec.authors = ["Aleksey V Zapparov"]
10
+ spec.email = ["ixti@member.fsf.org"]
11
+ spec.summary = "cachew-#{Cachew::VERSION}"
12
+ spec.description = "Unified cache interface."
13
+ spec.homepage = "https://github.com/ixti/cachew"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ end
@@ -0,0 +1,19 @@
1
+ require "cachew/version"
2
+ require "cachew/adapters"
3
+
4
+ # Unified cache interface
5
+ class Cachew
6
+ extend Forwardable
7
+
8
+ attr_reader :adapter
9
+
10
+ def initialize(store = nil)
11
+ @adapter = Adapters.build_adapter_for store
12
+ end
13
+
14
+ def fetch(key)
15
+ has?(key) ? get(key) : set(key, yield)
16
+ end
17
+
18
+ def_delegators :adapter, :set, :get, :has?
19
+ end
@@ -0,0 +1,15 @@
1
+ require "cachew/adapters/base_adapter"
2
+ require "cachew/adapters/hash_adapter"
3
+ require "cachew/adapters/null_adapter"
4
+
5
+ class Cachew
6
+ module Adapters
7
+ def self.build_adapter_for(store)
8
+ case store
9
+ when BaseAdapter then store
10
+ when Hash then HashAdapter.new(store)
11
+ else NullAdapter.new
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ class Cachew
2
+ module Adapters
3
+ BaseAdapter = Struct.new(:store)
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ class Cachew
2
+ module Adapters
3
+ class HashAdapter < BaseAdapter
4
+ def set(key, value)
5
+ store[key] = value
6
+ end
7
+
8
+ def get(key)
9
+ store[key]
10
+ end
11
+
12
+ def has?(key)
13
+ store.key?(key)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ class Cachew
2
+ module Adapters
3
+ class NullAdapter < BaseAdapter
4
+ def set(_, value)
5
+ value
6
+ end
7
+
8
+ def get(_)
9
+ nil
10
+ end
11
+
12
+ def has?(_)
13
+ false
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class Cachew
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Cachew::Adapters::HashAdapter do
4
+ let(:adapter) { described_class.new({}) }
5
+
6
+ describe "#set" do
7
+ it "returns back given value" do
8
+ expect(adapter.set(:foo, :bar)).to be :bar
9
+ end
10
+
11
+ it "sets value in the underlying store Hash" do
12
+ adapter.set(:foo, :bar)
13
+ expect(adapter.store).to eq :foo => :bar
14
+ end
15
+ end
16
+
17
+ describe "#get" do
18
+ it "returns nil if value does not exists" do
19
+ expect(adapter.get(:foo)).to be_nil
20
+ end
21
+
22
+ it "returns saved value if exists" do
23
+ adapter.store[:foo] = :bar
24
+ expect(adapter.get(:foo)).to be :bar
25
+ end
26
+ end
27
+
28
+ describe "#has?" do
29
+ it "returns true even if stored value is nil" do
30
+ adapter.store[:foo] = nil
31
+ expect(adapter.has?(:foo)).to be_true
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ describe Cachew::Adapters::NullAdapter do
4
+ let(:adapter) { described_class.new }
5
+
6
+ describe "#set" do
7
+ it "returns back given value" do
8
+ expect(adapter.set(:foo, :bar)).to be :bar
9
+ end
10
+ end
11
+
12
+ describe "#get" do
13
+ it "always returns nil" do
14
+ adapter.set(:foo, :bar)
15
+ expect(adapter.get :foo).to be_nil
16
+ end
17
+ end
18
+
19
+ describe "#has?" do
20
+ it "always returns false" do
21
+ adapter.set(:foo, :bar)
22
+ expect(adapter.has? :foo).to be_false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Cachew::Adapters do
4
+ describe ".build_adapter_for" do
5
+ context "with Hash instance" do
6
+ it "returns HashAdapter" do
7
+ adapter = described_class.build_adapter_for :foo => :bar
8
+ expect(adapter).to be_a Cachew::Adapters::HashAdapter
9
+ end
10
+ end
11
+
12
+ context "with instance of BaseAdapter" do
13
+ it "returns adapter as is" do
14
+ adapter = Cachew::Adapters::HashAdapter.new :foo => :bar
15
+ expect(described_class.build_adapter_for adapter).to be adapter
16
+ end
17
+ end
18
+
19
+ it "returns NullAdapter if can't find better candidate" do
20
+ expect(described_class.build_adapter_for "test")
21
+ .to be_a Cachew::Adapters::NullAdapter
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe Cachew do
4
+ describe ".new" do
5
+ it "initiates with NullAdapter by default" do
6
+ expect(described_class.new.adapter).to be_a Cachew::Adapters::BaseAdapter
7
+ end
8
+
9
+ it "calls Adapters.build_adapter_for with given store" do
10
+ store = { :foo => :bar }
11
+ expect(Cachew::Adapters).to receive(:build_adapter_for).with store
12
+ described_class.new store
13
+ end
14
+ end
15
+
16
+ describe "#set" do
17
+ subject(:cachew) { described_class.new.set }
18
+ specify { expect { cachew.adapter.to receive :set } }
19
+ end
20
+
21
+ describe "#get" do
22
+ subject(:cachew) { described_class.new.get }
23
+ specify { expect { cachew.adapter.to receive :get } }
24
+ end
25
+
26
+ describe "#has?" do
27
+ subject(:cachew) { described_class.new.has? }
28
+ specify { expect { cachew.adapter.to receive :has? } }
29
+ end
30
+
31
+ describe "#fetch" do
32
+ let(:cachew) { described_class.new :foo => :bar }
33
+
34
+ it "calls has?" do
35
+ expect(cachew).to receive(:has?)
36
+ cachew.fetch(:foo) { :moo }
37
+ end
38
+
39
+ context "when adapter has? key" do
40
+ it "calls get" do
41
+ expect(cachew).to receive(:get)
42
+ cachew.fetch(:foo) { :moo }
43
+ end
44
+ end
45
+
46
+ context "when adapter does not has? key" do
47
+ it "calls set" do
48
+ expect(cachew).to receive(:set)
49
+ cachew.fetch(:bar) { :moo }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,23 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+
4
+
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
6
+ SimpleCov::Formatter::HTMLFormatter,
7
+ Coveralls::SimpleCov::Formatter
8
+ ]
9
+
10
+
11
+ SimpleCov.start do
12
+ add_filter "/spec/"
13
+ end
14
+
15
+
16
+ require "cachew"
17
+
18
+
19
+ RSpec.configure do |config|
20
+ config.expect_with :rspec do |c|
21
+ c.syntax = :expect
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cachew
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksey V Zapparov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-02-02 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: "1.5"
22
+ type: :development
23
+ version_requirements: *id001
24
+ description: Unified cache interface.
25
+ email:
26
+ - ixti@member.fsf.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - .gitignore
35
+ - .rubocop.yml
36
+ - Gemfile
37
+ - Guardfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - cachew.gemspec
42
+ - lib/cachew.rb
43
+ - lib/cachew/adapters.rb
44
+ - lib/cachew/adapters/base_adapter.rb
45
+ - lib/cachew/adapters/hash_adapter.rb
46
+ - lib/cachew/adapters/null_adapter.rb
47
+ - lib/cachew/version.rb
48
+ - spec/lib/cachew/adapters/hash_adapter_spec.rb
49
+ - spec/lib/cachew/adapters/null_adapter_spec.rb
50
+ - spec/lib/cachew/adapters_spec.rb
51
+ - spec/lib/cachew_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/ixti/cachew
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - &id002
66
+ - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - *id002
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 2.1.10
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: cachew-0.1.0
79
+ test_files:
80
+ - spec/lib/cachew/adapters/hash_adapter_spec.rb
81
+ - spec/lib/cachew/adapters/null_adapter_spec.rb
82
+ - spec/lib/cachew/adapters_spec.rb
83
+ - spec/lib/cachew_spec.rb
84
+ - spec/spec_helper.rb
85
+ has_rdoc: