wireless_model 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Phil Smith
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = wireless_model
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Phil Smith. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "wireless_model"
8
+ gem.summary = %Q{Use models indifferently through either active record or active resource}
9
+ gem.description = %Q{Use models indifferently through either active record or active resource}
10
+ gem.email = "phil.h.smith@gmail.com"
11
+ gem.homepage = "http://github.com/phs/wireless_model"
12
+ gem.authors = ["Phil Smith"]
13
+ gem.add_dependency "activesupport", ">= 3.0.0.beta2"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "wireless_model #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,9 @@
1
+ module ActiveResource
2
+ module ActsLikeActiveRecord
3
+
4
+ def destroyed?
5
+ !(new_record? || exists?)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ module ActiveResource
4
+ module Scopes
5
+ def scoped
6
+ ActiveResource::Scope.new(self)
7
+ end
8
+
9
+ delegate :where, :to => :scoped
10
+
11
+ def scopes
12
+ @scopes ||= {}
13
+ end
14
+
15
+ def scope(name, *args, &block)
16
+ scopes[name = name.to_sym] = (args.first || block || lambda { |*args| where name => args })
17
+ self.class.delegate name, :to => :scoped
18
+ end
19
+ end
20
+
21
+ class Scope
22
+ include Enumerable
23
+
24
+ attr_reader :resource_class
25
+
26
+ def initialize(resource_class)
27
+ @resource_class = resource_class
28
+ end
29
+
30
+ def params
31
+ @params ||= {}
32
+ end
33
+
34
+ def to_a
35
+ resource_class.find :all, :params => params
36
+ end
37
+
38
+ delegate :each, :to => :to_a
39
+
40
+ def where(params = {})
41
+ dup.tap { |scope| scope.params.update params }
42
+ end
43
+
44
+ delegate :scopes, :to => :resource_class
45
+
46
+ def method_missing(name, *args)
47
+ return super unless scopes.key? name
48
+ scope = scopes[name]
49
+ scope = scope.call(*args) if scope.is_a? Proc
50
+
51
+ where scope.params
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_support/core_ext/module/introspection'
2
+ require 'active_resource/acts_like_active_record'
3
+ require 'active_resource/scopes'
4
+
5
+ module WirelessModel
6
+
7
+ def self.included(mod)
8
+ mod.extend ClassMethods
9
+
10
+ if defined? ActiveResource::Base
11
+ ActiveResource::Base.send :include, ActiveResource::ActsLikeActiveRecord
12
+ ActiveResource::Base.extend ActiveResource::Scopes
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def record_module(*args)
18
+ @record_module = args.first unless args.empty?
19
+ (@record_module || parents.first.name).constantize rescue nil
20
+ end
21
+
22
+ def resource_module(*args)
23
+ @resource_module = args.first unless args.empty?
24
+ (@resource_module || "#{parents.first}::Resources").constantize rescue nil
25
+ end
26
+
27
+ def const_missing(sym)
28
+ return record_module.const_get(sym) rescue
29
+ return resource_module.const_get(sym) rescue
30
+ super
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class MyResource
4
+ include ::ActiveResource::ActsLikeActiveRecord
5
+
6
+ def new_record?
7
+ false
8
+ end
9
+
10
+ def exists?
11
+ true
12
+ end
13
+ end
14
+
15
+ describe ActiveResource::ActsLikeActiveRecord do
16
+
17
+ before :each do
18
+ @resource = MyResource.new
19
+ end
20
+
21
+ describe '#destroyed?' do
22
+ it "fails if the resource is new" do
23
+ @resource.should_receive(:new_record?).and_return(true)
24
+ @resource.should_not be_destroyed
25
+ end
26
+
27
+ it "fails if the resource is exists" do
28
+ @resource.should_receive(:exists?).and_return(true)
29
+ @resource.should_not be_destroyed
30
+ end
31
+
32
+ it "holds if the resource is not new, and doesn't exist" do
33
+ @resource.should_receive(:new_record?).and_return(false)
34
+ @resource.should_receive(:exists?).and_return(false)
35
+ @resource.should be_destroyed
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,162 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class MyResource
4
+ extend ActiveResource::Scopes
5
+
6
+ def find(what, options = {})
7
+ end
8
+ end
9
+
10
+ describe ActiveResource::Scope do
11
+ before :each do
12
+ @scope = ActiveResource::Scope.new(MyResource)
13
+ end
14
+
15
+ describe '#resource_class' do
16
+ it 'is the type passed into the constructor' do
17
+ ActiveResource::Scope.new(MyResource).resource_class.should == MyResource
18
+ end
19
+ end
20
+
21
+ describe '#params' do
22
+ it 'is a hash' do
23
+ @scope.params.should be_a Hash
24
+ end
25
+
26
+ it 'begins empty' do
27
+ @scope.params.should be_empty
28
+ end
29
+ end
30
+
31
+ describe '#to_a' do
32
+ it 'calls and returns #find on #resource_class passing in #params' do
33
+ MyResource.should_receive(:find).with(:all, :params => { :foo => :bar }).and_return(:array)
34
+ @scope.params[:foo] = :bar
35
+ @scope.to_a.should == :array
36
+ end
37
+ end
38
+
39
+ it 'is Enumerable' do
40
+ @scope.should be_a Enumerable
41
+ end
42
+
43
+ it 'delegates #each to #to_a' do
44
+ @scope.stub!(:to_a).and_return([:a, :b, :c])
45
+ @scope.collect(&:to_s).should == %w{a b c}
46
+ end
47
+
48
+ it 'delegates #scopes to #resource_class' do
49
+ @scope.should_receive(:resource_class).and_return(klass = mock("resource class"))
50
+ klass.should_receive(:scopes)
51
+ @scope.scopes
52
+ end
53
+
54
+ describe '#where' do
55
+ it "dups the scope and updates the copy's params with its arguments" do
56
+ refined = @scope.where :foo => :bar
57
+ @scope.params.should be_empty
58
+ refined.params[:foo].should == :bar
59
+ end
60
+ end
61
+
62
+ describe '#method_missing', 'called with a scope name' do
63
+ before :all do
64
+ MyResource.instance_eval do
65
+ scope :yellow, where(:color => :yellow)
66
+ scope :published, where(:published => true)
67
+ scope :colored, lambda { |color|
68
+ where :color => color
69
+ }
70
+ end
71
+ end
72
+
73
+ it 'is the named scope' do
74
+ yellow = MyResource.scoped.yellow
75
+ yellow.should be_a ActiveResource::Scope
76
+ yellow.params[:color].should == :yellow
77
+ end
78
+
79
+ it 'calls the lambda if the scope is one, passing along any extra arguments' do
80
+ MyResource.scoped.colored(:red).params[:color].should == :red
81
+ end
82
+
83
+ it 'can chain scopes' do
84
+ MyResource.scoped.colored(:blue).published.params.should == { :color => :blue, :published => true }
85
+ end
86
+ end
87
+ end
88
+
89
+ describe ActiveResource::Scopes do
90
+ before :each do
91
+ @resource = MyResource.new
92
+ end
93
+
94
+ describe '.scoped' do
95
+ it 'is a scope' do
96
+ MyResource.scoped.should be_a ActiveResource::Scope
97
+ end
98
+
99
+ it 'has the originating class for its resource class' do
100
+ MyResource.scoped.resource_class.should == MyResource
101
+ end
102
+
103
+ it 'is not cached between calls' do
104
+ # equal? tests object identity (i.e. memory address)
105
+ MyResource.scoped.should_not be_equal(MyResource.scoped)
106
+ end
107
+ end
108
+
109
+ describe '.where' do
110
+ it 'delegates to .scoped' do
111
+ MyResource.should_receive(:scoped).and_return(scope = mock("new scope"))
112
+ scope.should_receive(:where).with(:foo => :bar).and_return(:refined_scope)
113
+ MyResource.where(:foo => :bar).should == :refined_scope
114
+ end
115
+ end
116
+
117
+ describe '.scopes' do
118
+ it 'is a hash' do
119
+ MyResource.scopes.should be_a Hash
120
+ end
121
+ end
122
+
123
+ describe '.scope' do
124
+ it "takes a name and a scope, which it adds to .scopes" do
125
+ MyResource.scope :recent, scope = mock("a scope")
126
+ MyResource.scopes[:recent].should == scope
127
+ end
128
+
129
+ it "treats string names as symbols" do
130
+ MyResource.scope 'unpublished', scope = mock("a scope")
131
+ MyResource.scopes[:unpublished].should == scope
132
+ end
133
+
134
+ it "can take a block as the 2nd parameter" do
135
+ published = lambda {}
136
+ MyResource.scope :published, &published
137
+ MyResource.scopes[:published].should == published
138
+ end
139
+
140
+ it 'delegates the scope name to #scoped' do
141
+ MyResource.instance_eval do
142
+ scope :owned_by, lambda { |user|
143
+ where :owner => user
144
+ }
145
+ end
146
+
147
+ MyResource.owned_by(:me).params[:owner].should == :me
148
+ end
149
+
150
+ describe 'with one parameter' do
151
+ before :all do
152
+ MyResource.instance_eval do
153
+ scope :flavor
154
+ end
155
+ end
156
+
157
+ it 'restricts with the scope name, by the passed parameters' do
158
+ MyResource.flavor(:apple, :cinnamon).params[:flavor].should == [:apple, :cinnamon]
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ gem 'activesupport'
6
+
7
+ require 'wireless_model'
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+
11
+ Spec::Runner.configure do |config|
12
+
13
+ end
@@ -0,0 +1,89 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module MyApp
4
+ module Client
5
+ include WirelessModel
6
+ end
7
+
8
+ class MyModel
9
+ end
10
+
11
+ module Resources
12
+ class MyModel
13
+ end
14
+
15
+ class OnlyResourceModel
16
+ end
17
+ end
18
+
19
+ module Models; end
20
+ module OtherResources; end
21
+ end
22
+
23
+ describe WirelessModel do
24
+ describe '.record_module' do
25
+ it 'is the parent module by default' do
26
+ MyApp::Client.record_module.should == MyApp
27
+ end
28
+
29
+ it 'sets itself to the module named by its parameter, if any' do
30
+ MyApp::Client.record_module('MyApp::Models').should == MyApp::Models
31
+ MyApp::Client.record_module.should == MyApp::Models
32
+ end
33
+
34
+ it 'becomes the default if passed nil' do
35
+ MyApp::Client.record_module(nil)
36
+ MyApp::Client.record_module.should == MyApp
37
+ end
38
+
39
+ it 'is nil if the module does not exist' do
40
+ MyApp::Client.record_module('MyApp::ChezBurgers')
41
+ MyApp::Client.record_module.should be_nil
42
+ end
43
+
44
+ after :all do
45
+ MyApp::Client.record_module nil
46
+ end
47
+ end
48
+
49
+ describe '.resource_module' do
50
+ it 'is the Resources sibling module by default' do
51
+ MyApp::Client.resource_module.should == MyApp::Resources
52
+ end
53
+
54
+ it 'sets itself to the module named by its parameter, if any' do
55
+ MyApp::Client.resource_module('MyApp::OtherResources').should == MyApp::OtherResources
56
+ MyApp::Client.resource_module.should == MyApp::OtherResources
57
+ end
58
+
59
+ it 'becomes the default if passed nil' do
60
+ MyApp::Client.resource_module(nil)
61
+ MyApp::Client.resource_module.should == MyApp::Resources
62
+ end
63
+
64
+ it 'is nil if the module does not exist' do
65
+ MyApp::Client.resource_module('MyApp::ChezBurgers')
66
+ MyApp::Client.resource_module.should be_nil
67
+ end
68
+
69
+ after :all do
70
+ MyApp::Client.resource_module nil
71
+ end
72
+ end
73
+
74
+ describe '.const_missing' do
75
+ it 'first looks for the missing constant in .record_model' do
76
+ MyApp::Client::MyModel.should == MyApp::MyModel
77
+ end
78
+
79
+ it 'next looks for the missing constant in .resource_model' do
80
+ MyApp::Client::OnlyResourceModel.should == MyApp::Resources::OnlyResourceModel
81
+ end
82
+
83
+ it 'finally just defaults to super' do
84
+ lambda do
85
+ MyApp::Client::ChezBurger
86
+ end.should raise_error NameError, "uninitialized constant MyApp::Client::ChezBurger"
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wireless_model}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Phil Smith"]
12
+ s.date = %q{2010-05-21}
13
+ s.description = %q{Use models indifferently through either active record or active resource}
14
+ s.email = %q{phil.h.smith@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/active_resource/acts_like_active_record.rb",
27
+ "lib/active_resource/scopes.rb",
28
+ "lib/wireless_model.rb",
29
+ "spec/active_resource/acts_like_active_record_spec.rb",
30
+ "spec/active_resource/scopes_spec.rb",
31
+ "spec/spec.opts",
32
+ "spec/spec_helper.rb",
33
+ "spec/wireless_model_spec.rb",
34
+ "wireless_model.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/phs/wireless_model}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{Use models indifferently through either active record or active resource}
41
+ s.test_files = [
42
+ "spec/active_resource/acts_like_active_record_spec.rb",
43
+ "spec/active_resource/scopes_spec.rb",
44
+ "spec/spec_helper.rb",
45
+ "spec/wireless_model_spec.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta2"])
54
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
55
+ else
56
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta2"])
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<activesupport>, [">= 3.0.0.beta2"])
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ end
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wireless_model
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Phil Smith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-21 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta2
32
+ version: 3.0.0.beta2
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 2
45
+ - 9
46
+ version: 1.2.9
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Use models indifferently through either active record or active resource
50
+ email: phil.h.smith@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - .document
60
+ - .gitignore
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - lib/active_resource/acts_like_active_record.rb
66
+ - lib/active_resource/scopes.rb
67
+ - lib/wireless_model.rb
68
+ - spec/active_resource/acts_like_active_record_spec.rb
69
+ - spec/active_resource/scopes_spec.rb
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb
72
+ - spec/wireless_model_spec.rb
73
+ - wireless_model.gemspec
74
+ has_rdoc: true
75
+ homepage: http://github.com/phs/wireless_model
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Use models indifferently through either active record or active resource
104
+ test_files:
105
+ - spec/active_resource/acts_like_active_record_spec.rb
106
+ - spec/active_resource/scopes_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/wireless_model_spec.rb