live_resource-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
20
+ test/dummy/db/*.sqlite3
21
+ test/dummy/log/*.log
22
+ test/dummy/tmp/
23
+ test/dummy/.sass-cache
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ live_resource-rails
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p392
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in live_resource-rails.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'ruby-debug'
18
+
19
+ gem 'live_resource', path: '/Users/Will/Projects/Balanced Tree/live_resource'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Will Madden
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.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # LiveResource::Rails
2
+
3
+ Rails does not have an elegant abstraction for defining resources that exist between requests.
4
+
5
+ Requesting a resource, for example, /profiles.json, is handled very well, and will route through the ProfilesController,
6
+ load the collection of Profile models, and render the index view (MVC).
7
+
8
+ The LiveResource gem adds the concept of a Resource, which is an object derived from server state (such as models) that
9
+ can be *pulled* (requested, as in the above example), or *pushed* when changes to server state are detected.
10
+
11
+ LiveResource-Rails adds support to Rails for defining and configuring live resources.
12
+
13
+ ## Example
14
+
15
+ ```ruby
16
+ # app/controllers/profiles_controller.rb
17
+
18
+ class ProfilesController < ApplicationController
19
+ ...
20
+
21
+ # show.json
22
+ # {
23
+ # name: @profile.name,
24
+ # avatar: {
25
+ # alt_text: @profile.avatar.alt_text,
26
+ # url: avatar_url( @profile.avatar )
27
+ # }
28
+ # }
29
+ def show
30
+ ...
31
+ end
32
+
33
+ live_resource :show do
34
+ identifier { |profile| profile_path(profile) }
35
+
36
+ # When a Profile instance is changed (requires live_resource-activerecord)
37
+ depends_on(Profile) do |profile|
38
+ # Push an update for the resource belonging to the profile
39
+ push(profile)
40
+ end
41
+
42
+ # Since the view will change if the avatar changes, depend on that too
43
+ depends_on(Avatar) do |avatar|
44
+ push avatar.profile
45
+ end
46
+ end
47
+
48
+ ...
49
+ end
50
+ ```
51
+
52
+ ## Installation
53
+
54
+ Add this line to your application's Gemfile:
55
+
56
+ gem 'live_resource-rails'
57
+
58
+ And then execute:
59
+
60
+ $ bundle
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install live_resource-rails
65
+
66
+ ## Usage
67
+
68
+ TODO: Write usage instructions here
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
77
+
78
+ ## TODO
79
+
80
+ - Integration test with dummy Rails app (also as an example for getting started)
81
+ - Test injection of URL helpers in DSL into Builder
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env rake
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ require "bundler/gem_tasks"
17
+ require 'rake/testtask'
18
+
19
+ RDoc::Task.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'LiveResourceRails'
22
+ rdoc.options << '--line-numbers'
23
+ rdoc.rdoc_files.include('README.rdoc')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+ task :default => :test
@@ -0,0 +1,57 @@
1
+ require "live_resource/builder"
2
+
3
+ module LiveResource
4
+ module Rails
5
+ module ControllerDSL
6
+
7
+ def self.included(base)
8
+ base.extend(InstanceMethods)
9
+ end
10
+
11
+ module InstanceMethods
12
+ def live_resource(resource_identifier, &block)
13
+ config = ::Rails.configuration.live_resource if ::Rails.configuration.respond_to?(:live_resource)
14
+ raise 'You must configure config.live_resource (e.g. in application.rb)' unless config
15
+
16
+ protocol = config[:protocol]
17
+ raise 'You must set config.live_resource.protocol (e.g. in application.rb)' unless protocol
18
+
19
+ dependency_types = ::Rails.configuration.live_resource[:dependency_types]
20
+ raise 'You must set config.live_resource.dependency_types (e.g. in application.rb)' unless dependency_types
21
+
22
+ # Set up the builder
23
+ builder = Builder.new(resource_identifier, protocol, dependency_types, Module.new do
24
+ # TODO: This is added for convenience, so that in the execution scope of the identifier block we'll have
25
+ # access to path helpers. This needs to be refactored into something more elegant.
26
+ include ::Rails.application.routes.url_helpers
27
+
28
+ def default_url_options
29
+ ::Rails.application.routes.default_url_options
30
+ end
31
+ end)
32
+
33
+ block.call(builder)
34
+
35
+ # Grab the result
36
+ resource = builder.resource
37
+
38
+ # Create a method on the controller to allow access to the Resource
39
+ self.instance_eval do
40
+ define_method(:"#{resource.name}_resource", Proc.new { resource })
41
+ end
42
+
43
+ # Create a helper method to allow views to access the resource ID
44
+ ActionController::Base.helper do
45
+ define_method(:"live_#{builder.resource.name}_id") do |*context|
46
+ resource.identifier(*context)
47
+ end
48
+ end
49
+
50
+ # Activate the dependencies
51
+ resource.dependencies.each { |dependency| dependency.watch }
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,8 @@
1
+ require 'live_resource/rails'
2
+ require "live_resource/rails/controller_dsl"
3
+
4
+ require 'action_controller'
5
+
6
+ ActionController::Base.class_eval do
7
+ include LiveResource::Rails::ControllerDSL
8
+ end
@@ -0,0 +1,5 @@
1
+ module LiveResource
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "live_resource/rails/version"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'live_resource/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "live_resource-rails"
8
+ spec.version = LiveResource::Rails::VERSION
9
+ spec.authors = ["Will Madden"]
10
+ spec.email = ["will@letsgeddit.com"]
11
+ spec.description = %q{Convenience methods for using LiveResource in Rails applications}
12
+ spec.summary = %q{Convenience methods for using LiveResource in Rails applications}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency "rails", "~> 3.2.13"
22
+ spec.add_dependency 'live_resource'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,152 @@
1
+ require "spec_helper"
2
+ require "action_controller/base"
3
+ require 'live_resource/protocol'
4
+ require 'live_resource/dependency'
5
+ require 'rails'
6
+
7
+ include LiveResource::Rails
8
+
9
+ describe LiveResource::Rails::ControllerDSL do
10
+
11
+ let(:controller_class) do
12
+ class DummyController < ActionController::Base
13
+ include ControllerDSL
14
+ end
15
+ end
16
+
17
+ let(:url_helpers_module) do
18
+ module URLHelpers
19
+ end
20
+ URLHelpers
21
+ end
22
+
23
+ before do
24
+ Rails.stub_chain(:application, :routes, :url_helpers).and_return(url_helpers_module)
25
+ end
26
+
27
+ describe "#live_resource" do
28
+
29
+ subject do
30
+ b = block # let'ed methods are lost in the scope of the proc
31
+ controller_class.class_eval(&Proc.new { live_resource(:some_thing, &b) })
32
+ end
33
+
34
+ let(:controller) { DummyController.new }
35
+ let(:builder) do
36
+ double(LiveResource::Builder,
37
+ resource_method: resource_method,
38
+ resource: resource)
39
+ end
40
+ let(:block) { Proc.new {} }
41
+ let(:resource_method) { Proc.new {} }
42
+ let(:resource) { double(LiveResource::Resource, name: resource_name) }
43
+ let(:resource_name) { :some_method }
44
+
45
+ before do
46
+ LiveResource::Builder.stub(:new).and_return(builder)
47
+ ActionController::Base.stub(:helper)
48
+ end
49
+
50
+ context 'when live resource is not configured' do
51
+ before do
52
+ Rails.stub(:configuration)
53
+ end
54
+
55
+ it 'should raise an error' do
56
+ expect(-> { subject }).to raise_error('You must configure config.live_resource (e.g. in application.rb)')
57
+ end
58
+ end
59
+
60
+ context 'when live resource is configured' do
61
+ let(:protocol) {}
62
+ let(:dependency_types) {}
63
+
64
+ let(:live_resource_configuration) do
65
+ {
66
+ protocol: protocol,
67
+ dependency_types: dependency_types
68
+ }
69
+ end
70
+
71
+ before do
72
+ Rails.stub_chain(:configuration, :live_resource).and_return(live_resource_configuration)
73
+ end
74
+
75
+ context 'when the protocol is not configured' do
76
+ it 'should raise an error' do
77
+ expect(-> { subject }).to raise_error('You must set config.live_resource.protocol (e.g. in application.rb)')
78
+ end
79
+ end
80
+
81
+ context 'when the protocol is configured' do
82
+ let(:protocol) { double(LiveResource::Protocol) }
83
+
84
+ context 'but the supported dependency types are not set' do
85
+ it 'should raise an error' do
86
+ expected_error = 'You must set config.live_resource.dependency_types (e.g. in application.rb)'
87
+ expect(-> { subject }).to raise_error(expected_error)
88
+ end
89
+ end
90
+
91
+ context 'and the supported dependency types are set' do
92
+ let(:dependency_types) { [double(LiveResource::Dependency)] }
93
+
94
+ let(:dependencies) { [] }
95
+
96
+ before do
97
+ resource.stub(dependencies: dependencies)
98
+ end
99
+
100
+ it "should instantiate a new builder" do
101
+ LiveResource::Builder.should_receive(:new).with(:some_thing, protocol, dependency_types,
102
+ an_instance_of(Module))
103
+ subject
104
+ end
105
+
106
+ it "should call the block with the builder" do
107
+ block.should_receive(:call).with(builder)
108
+ subject
109
+ end
110
+
111
+ it "should define a new method on the controller" do
112
+ controller_class.should_receive(:define_method).with(:"#{resource.name}_resource", instance_of(Proc))
113
+ subject
114
+ end
115
+
116
+ describe "the method" do
117
+ before do
118
+ _proc = nil
119
+ controller_class.stub(:define_method) { |name, proc| _proc = proc }
120
+ subject
121
+ @proc = _proc
122
+ end
123
+
124
+ let(:method) { @proc }
125
+
126
+ it 'should return the resource' do
127
+ expect(method.call).to be resource
128
+ end
129
+ end
130
+
131
+ it "should define a helper method for the identifier" do
132
+ ActionController::Base.should_receive(:helper)
133
+ subject
134
+ end
135
+
136
+ context 'when the block defines some dependencies' do
137
+ let(:dependencies) { [dependency1, dependency2] }
138
+ let(:dependency1) { double(LiveResource::Dependency, watch: nil) }
139
+ let(:dependency2) { double(LiveResource::Dependency, watch: nil) }
140
+
141
+ it 'should activate each dependency' do
142
+ dependency1.should_receive(:watch)
143
+ dependency2.should_receive(:watch)
144
+ subject
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ end
152
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ def require_tree(path)
4
+ path = File.expand_path(path, File.dirname(__FILE__))
5
+
6
+ files = Dir.glob(File.join(path, '**/*.rb'))
7
+ raise "Directory '#{path}' is empty" unless files.length > 0
8
+
9
+ files.each { |file| puts " require #{file}"; require file }
10
+ end
11
+
12
+ require_tree '../lib'
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: live_resource-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Will Madden
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.13
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.13
30
+ - !ruby/object:Gem::Dependency
31
+ name: live_resource
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Convenience methods for using LiveResource in Rails applications
111
+ email:
112
+ - will@letsgeddit.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .ruby-gemset
119
+ - .ruby-version
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/live_resource/rails.rb
125
+ - lib/live_resource/rails/controller_dsl.rb
126
+ - lib/live_resource/rails/init.rb
127
+ - lib/live_resource/rails/version.rb
128
+ - live_resource-rails.gemspec
129
+ - spec/live_resource/rails/controller_dsl_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: 1742430517839249327
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ segments:
154
+ - 0
155
+ hash: 1742430517839249327
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.25
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Convenience methods for using LiveResource in Rails applications
162
+ test_files:
163
+ - spec/live_resource/rails/controller_dsl_spec.rb
164
+ - spec/spec_helper.rb