active_resource_extensions 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Active Record Extensions
2
+
3
+ Some extensions we are using for [Doorkeeper](http://www.doorkeeper.jp). Still very basic and tailored to our needs. Feel free to extend and make more customizable.
@@ -1,9 +1,13 @@
1
1
  require 'active_resource_extensions/railtie'
2
2
  module ActiveResourceExtensions
3
3
  autoload :GracefulErrors, "active_resource_extensions/graceful_errors"
4
+ autoload :Caching, "active_resource_extensions/caching"
4
5
  def inherited(subclass)
5
6
  subclass.class_eval do
6
7
  class << self
8
+ # Caching is included before GracefulErrors to avoid caching error
9
+ # results
10
+ include ActiveResourceExtensions::Caching
7
11
  include ActiveResourceExtensions::GracefulErrors
8
12
  end
9
13
  end
@@ -0,0 +1,10 @@
1
+ # To use this, you need config.cache_classes to be set to true.
2
+ module ActiveResourceExtensions
3
+ module Caching
4
+ private
5
+
6
+ def find_every(options)
7
+ Rails.cache.fetch(to_param, :expires_in => 1.hour) { super }
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveResourceExtensions
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveResourceExtensions::Caching do
4
+ before do
5
+ @resource = mock_resource(ActiveResourceExtensions::Caching)
6
+ @resource.proc = lambda { [] }
7
+ Rails.stub!(:cache).and_return(mock("cache"))
8
+ end
9
+
10
+ it "all should return the same value" do
11
+ @resource.should_receive(:to_param).and_return("foo")
12
+ Rails.cache.should_receive(:fetch).with("foo", :expires_in => 1.hour)
13
+ @resource.all
14
+ end
15
+ end
@@ -3,40 +3,25 @@ require 'active_resource'
3
3
 
4
4
  describe ActiveResourceExtensions::GracefulErrors do
5
5
  before do
6
- mock = Class.new(ActiveResource::Base)
7
- mock.class_eval do
8
- class << self
9
- attr_accessor :exception
10
-
11
- def find_every(option)
12
- raise exception
13
- end
14
- end
15
- end
16
- @resource = Class.new(mock)
17
- @resource.class_eval do
18
- class << self
19
- include ActiveResourceExtensions::GracefulErrors
20
- end
21
- end
6
+ @resource = mock_resource(ActiveResourceExtensions::GracefulErrors)
22
7
  Rails.logger = mock("logger")
23
8
  Rails.logger.stub!(:fatal)
24
9
  end
25
10
 
26
11
  describe "Errno::ECONNREFUSED thrown" do
27
- before { @resource.exception = Errno::ECONNREFUSED }
12
+ before { @resource.proc = lambda { raise Errno::ECONNREFUSED } }
28
13
  it("first should return nil") { @resource.first.should be_nil }
29
14
  it("all should return empty array") { @resource.all.should be_empty }
30
15
  end
31
16
 
32
17
  describe "ActiveResource::TimeoutError thrown" do
33
- before { @resource.exception = ActiveResource::TimeoutError.new("message") }
18
+ before { @resource.proc = lambda { raise ActiveResource::TimeoutError.new("message") } }
34
19
  it("first should return nil") { @resource.first.should be_nil }
35
20
  it("all should return empty array") { @resource.all.should be_empty }
36
21
  end
37
22
 
38
23
  describe "arbitrary exception thrown" do
39
- before { @resource.exception = "arbitrary" }
24
+ before { @resource.proc = lambda { raise "arbitrary" } }
40
25
  it("first should raise exception") { lambda { @resource.first }.should raise_error }
41
26
  end
42
27
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,22 @@
1
1
  $:.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'active_resource_extensions'
3
+ require 'active_resource'
4
+
5
+ def mock_resource(mod)
6
+ mock = Class.new(ActiveResource::Base)
7
+ mock.class_eval do
8
+ class << self
9
+ attr_accessor :proc
10
+ def find_every(option)
11
+ proc.call
12
+ end
13
+ end
14
+ end
15
+ resource = Class.new(mock)
16
+ resource.class_eval(<<-EOD)
17
+ class << self
18
+ include #{mod}
19
+ end
20
+ EOD
21
+ resource
22
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_resource_extensions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paul McMahon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-10 00:00:00 +09:00
18
+ date: 2011-01-11 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -63,12 +63,15 @@ files:
63
63
  - .gitignore
64
64
  - Gemfile
65
65
  - Gemfile.lock
66
+ - README.md
66
67
  - Rakefile
67
68
  - active_resource_extensions.gemspec
68
69
  - lib/active_resource_extensions.rb
70
+ - lib/active_resource_extensions/caching.rb
69
71
  - lib/active_resource_extensions/graceful_errors.rb
70
72
  - lib/active_resource_extensions/railtie.rb
71
73
  - lib/active_resource_extensions/version.rb
74
+ - spec/active_resource_extensions/caching_spec.rb
72
75
  - spec/active_resource_extensions/graceful_errors_spec.rb
73
76
  - spec/spec_helper.rb
74
77
  has_rdoc: true
@@ -106,5 +109,6 @@ signing_key:
106
109
  specification_version: 3
107
110
  summary: Extensions to active resource
108
111
  test_files:
112
+ - spec/active_resource_extensions/caching_spec.rb
109
113
  - spec/active_resource_extensions/graceful_errors_spec.rb
110
114
  - spec/spec_helper.rb