autoload_resources 0.0.3

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: 2fd0f6f1956236b08235f03fe8eb30cc17ff99f6
4
+ data.tar.gz: 21300054280c5831049fe2913fb093362c30c3e7
5
+ SHA512:
6
+ metadata.gz: 80275e342a7e265f06165c3955dc1fc96e036b97c977edaaf20c842bed9e424f4d297f340f1cd7369308f531fca4230e61e950f91f143c0cd8b66fcac4757600
7
+ data.tar.gz: d7fab591a556f147229c9cad61e661886f4241cdff339fc11a491ca40b7d9d8c8cdf00adf6f1abf4ba12d00313e930f480657202215d847a345565a90eedd751
data/.gitignore ADDED
@@ -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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autoload_resources.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ end
10
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 barelyknown
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,29 @@
1
+ # AutoloadResources
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'autoload_resources'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install autoload_resources
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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
+
5
+ require 'autoload_resources/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "autoload_resources"
9
+ spec.version = AutoloadResources::VERSION
10
+ spec.authors = ["barelyknown"]
11
+ spec.email = ["sean@buytruckload.com"]
12
+ spec.description = %q{Autoload resources for Rails controllers}
13
+ spec.summary = %q{Autoload resources for Rails controllers}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_dependency "activesupport"
27
+ spec.add_dependency "controller_resource_class"
28
+ end
@@ -0,0 +1,10 @@
1
+ require "active_support/concern"
2
+ require "active_support/inflector"
3
+ require "active_support/core_ext/hash/indifferent_access"
4
+
5
+ require "autoload_resources/version"
6
+ require "autoload_resources/able"
7
+ require "autoload_resources/railtie" if defined?(Rails)
8
+
9
+ module AutoloadResources
10
+ end
@@ -0,0 +1,71 @@
1
+ module AutoloadResources
2
+ module Able
3
+ extend ActiveSupport::Concern
4
+
5
+ def autoload_resources(action_name=params[:action])
6
+ return unless self.class.autoload_procs[action_name]
7
+ self.class.autoload_instance_variable_names(action_name).each do |instance_variable_name|
8
+ instance_variable_set(
9
+ "@#{instance_variable_name}",
10
+ instance_eval(&(self.class.autoload_procs[action_name]))
11
+ )
12
+ end
13
+ end
14
+
15
+ def autoload_base_class
16
+ self.class.autoload_base_class
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def autoload_resources
22
+ set_default_autoload_procs
23
+ yield if block_given?
24
+ before_action :autoload_resources
25
+ end
26
+
27
+ def autoload_procs
28
+ @autoload_procs ||= HashWithIndifferentAccess.new
29
+ end
30
+
31
+ def autoload_instance_variable_name(klass, action_name)
32
+ case String(action_name)
33
+ when "index" then klass.model_name.plural
34
+ else klass.model_name.element
35
+ end
36
+ end
37
+
38
+ def autoload_instance_variable_names(action_name)
39
+ resource_class.ancestors.grep(Class).select do |klass|
40
+ klass != ActiveRecord::Base && klass.ancestors.include?(ActiveRecord::Base)
41
+ end.collect do |klass|
42
+ autoload_instance_variable_name(klass, action_name)
43
+ end
44
+ end
45
+
46
+ def set_default_autoload_procs
47
+ return unless resource_class
48
+ default_autoload_procs.each do |actions, closure|
49
+ for_actions(actions, closure)
50
+ end
51
+ end
52
+
53
+ def default_autoload_procs
54
+ {
55
+ index: Proc.new { resource_class.all },
56
+ new: Proc.new { resource_class.new },
57
+ create: Proc.new { resource_class.new(params[resource_class.model_name.element]) },
58
+ [:show, :edit, :update, :destroy] => Proc.new { resource_class.find(params[:id]) }
59
+ }
60
+ end
61
+
62
+ def for_actions(actions, proc=nil, &block)
63
+ Array(actions).each do |action|
64
+ autoload_procs[action] = block || proc
65
+ end
66
+ end
67
+ alias_method :for_action, :for_actions
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,9 @@
1
+ module AutoloadResources
2
+ class Railtie < Rails::Railtie
3
+ initializer "autoload_resources.action_controller" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include Able
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module AutoloadResources
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ class ActiveRecord
4
+ class Base; end
5
+ end
6
+
7
+ class Example < ActiveRecord::Base
8
+ attr_accessor :id
9
+ def self.new(id=1)
10
+ @id = id
11
+ end
12
+ def self.all
13
+ [new(1), new(2)]
14
+ end
15
+ def self.find(id=3)
16
+ new(id)
17
+ end
18
+ def ==(value)
19
+ id = value.id
20
+ end
21
+ end
22
+
23
+ class ExamplesController
24
+ include ControllerResourceClass::Able
25
+ include AutoloadResources::Able
26
+
27
+ def self.before_action(method)
28
+ end
29
+
30
+ def self.controller_name
31
+ "examples"
32
+ end
33
+
34
+ def params
35
+ { id: 3 }
36
+ end
37
+
38
+ end
39
+
40
+ describe ExamplesController do
41
+ let!(:model_name) do
42
+ double(:model_name, plural: "examples", element: "example")
43
+ end
44
+ before do
45
+ Example.stub(:model_name) { model_name }
46
+ ExamplesController.autoload_resources
47
+ end
48
+ it "should respond to autoload_resources" do
49
+ expect(described_class).to respond_to :autoload_resources
50
+ end
51
+
52
+ it "should know its base class" do
53
+ expect(described_class.resource_class).to eq Example
54
+ end
55
+
56
+ it "should set the default for the index action" do
57
+ subject.autoload_resources(:index)
58
+ expect(subject.instance_variable_get("@examples")).to eq Example.all
59
+ end
60
+
61
+ it "should set the default for the new action" do
62
+ subject.autoload_resources(:new)
63
+ expect(subject.instance_variable_get("@example")).to eq Example.new
64
+ end
65
+
66
+ # [:show, :edit, :update, :destroy].each do |action_name|
67
+ it "should set the default for the :show action" do
68
+ subject.autoload_resources(:show)
69
+ expect(subject.instance_variable_get("@example")).to eq Example.find
70
+ end
71
+ # end
72
+
73
+ it "can set a custom block for an action" do
74
+ described_class.autoload_resources do
75
+ described_class.for_action(:index) { [] }
76
+ end
77
+ subject.autoload_resources(:index)
78
+ expect(subject.instance_variable_get("@examples")).to eq []
79
+ end
80
+
81
+ end
@@ -0,0 +1,17 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+
4
+ require "autoload_resources"
5
+ require "controller_resource_class"
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoload_resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - barelyknown
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-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: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: controller_resource_class
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Autoload resources for Rails controllers
98
+ email:
99
+ - sean@buytruckload.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - Guardfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - autoload_resource.gemspec
112
+ - lib/autoload_resources.rb
113
+ - lib/autoload_resources/able.rb
114
+ - lib/autoload_resources/railtie.rb
115
+ - lib/autoload_resources/version.rb
116
+ - spec/lib/autoload_resources/able_spec.rb
117
+ - spec/spec_helper.rb
118
+ homepage: ''
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.0.2
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Autoload resources for Rails controllers
142
+ test_files:
143
+ - spec/lib/autoload_resources/able_spec.rb
144
+ - spec/spec_helper.rb