rails-and-solid 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails-and-solid.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,32 @@
1
+ # Installing
2
+
3
+ Configure the gem in your Gemfile:
4
+
5
+ gem 'rails-and-solid'
6
+
7
+ # Configuring
8
+
9
+ In order not to refactor Rails completely, the easiest solution is to just trick it
10
+ and make it believe you have the controller he wants, just trick him:
11
+
12
+ class ApplicationController < ActionController::Base
13
+
14
+ protect_from_forgery
15
+ include RailsAndSolid::TrickHim
16
+
17
+ end
18
+
19
+ # Using
20
+
21
+ NO INHERITANCE
22
+
23
+ GIVE A SIMPLE EXAMPLE
24
+
25
+ GIVE A LOAD EXAMPLE
26
+
27
+ GIVE AN AMAZING EXAMPLE
28
+
29
+ # Helpers
30
+
31
+ # Handlers
32
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec'
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.rspec_opts = ['--colour', '--format progress']
10
+ end
11
+
12
+ task :local_install => [:build] do
13
+ sh 'gem install -l pkg/*.gem'
14
+ end
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,14 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class Instantiate
4
+ def extract(controller, var)
5
+ val = var.camelize.constantize.new(controller.params[p])
6
+ controller.send :instance_variable_set ,"@#{var}", val
7
+ return val
8
+ end
9
+ def handles?(controller, name)
10
+ defined?(name.camelize.constantize)==true && name.camelize.constantize.is_a?(ActiveRecord::Base)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class KeyHandler
4
+ def initialize
5
+ @helpers = {}
6
+ end
7
+ def []=(name, who)
8
+ @helpers[name] = who
9
+ end
10
+ def extract(controller, name)
11
+ @helpers[name].new(controller)
12
+ end
13
+ def handles?(controller, name)
14
+ @helpers[name]
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class Load
4
+ def extract(controller, name)
5
+ var = type_for(name)
6
+ loaded = var.camelize.constantize.find(controller.params[:id])
7
+ controller.send :instance_variable_set ,"@#{var}", loaded
8
+ return loaded
9
+ end
10
+ def type_for(name)
11
+ name[7..-1]
12
+ end
13
+ def handles?(controller, name)
14
+ name[0..6]=="loaded_" && defined?(type_for(name).camelize.constantize)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class Param
4
+ def extract(controller, name)
5
+ controller.params[name]
6
+ end
7
+ def handles?(controller, name)
8
+ controller.params[name]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class ParamsAndScopes
4
+ def extract(controller, var)
5
+ if var=="params"
6
+ controller.params
7
+ elsif var=="session"
8
+ controller.session
9
+ else
10
+ controller.flash
11
+ end
12
+ end
13
+ SUPPORTED = ["params", "session", "flash"]
14
+ def handles?(controller, name)
15
+ SUPPORTED.include? name
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ class SessionLocator
4
+ def extract(controller, var)
5
+ controller.session[var]
6
+ end
7
+ def handles?(controller, name)
8
+ controller.session[name]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module RailsAndSolid
2
+ module Handler
3
+ autoload :KeyHandler, 'rails-and-solid/handler/key_handler'
4
+ autoload :ParamsAndScopes, 'rails-and-solid/handler/params_and_scopes'
5
+ autoload :Load, 'rails-and-solid/handler/load'
6
+ autoload :Param, 'rails-and-solid/handler/param'
7
+ autoload :Instantiate, 'rails-and-solid/handler/instantiate'
8
+ autoload :SessionLocator, 'rails-and-solid/handler/session_locator'
9
+ end
10
+
11
+ end
@@ -0,0 +1,12 @@
1
+ module RailsAndSolid
2
+ module Helper
3
+ class RedirectTo
4
+ def initialize(controller)
5
+ @controller = controller
6
+ end
7
+ def method_missing(path)
8
+ @controller.redirect_to @controller.send(path)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ module RailsAndSolid
2
+ module Helper
3
+ class Result
4
+ def initialize(controller)
5
+ @controller = controller
6
+ end
7
+
8
+ def redirect_to(where = nil)
9
+ if where
10
+ @controller.redirect_to @controller.send("#{where.to_s}_path")
11
+ else
12
+ RedirectTo.new(@controller)
13
+ end
14
+ end
15
+
16
+ def render(*args, &block)
17
+ @controller.render *args, &block
18
+ end
19
+
20
+ def text(content, options = {}, &block)
21
+ options[:text] = content
22
+ @controller.render(options, &block)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module RailsAndSolid
2
+ module Helper
3
+ autoload :RedirectTo, 'rails-and-solid/helper/redirect_to'
4
+ autoload :Result, 'rails-and-solid/helper/result'
5
+ end
6
+
7
+ end
@@ -0,0 +1,89 @@
1
+ module RailsAndSolid
2
+ module TrickHim
3
+
4
+ def method_for_action(method)
5
+ # TRICKing rails... no inheritance support
6
+ method
7
+ end
8
+
9
+ def class_exists?(class_name, method)
10
+ # TODO cache is only for production (no reload)
11
+ @exists ||= {}
12
+ return @exists[class_name] if @exists.key?(class_name)
13
+
14
+ # TODO solution1
15
+ klass = Module.const_get(class_name)
16
+ self.class.send :define_method, method do
17
+ # rails inheritance on my way (ret = super(sym)), once again...
18
+ end
19
+ return @exists[class_name] = klass.is_a?(Class)
20
+ rescue NameError
21
+ # exceptions and traces are expensive.
22
+ @exists[class_name] = false
23
+ return false
24
+ end
25
+
26
+ # TODO solution 2 is to define it to false, not overriden
27
+ # when you invoke hack, you say... to hell with inheritance
28
+ # def class_exists?(t)
29
+ # false
30
+ # end
31
+
32
+ def send_action(sym)
33
+ type = self.class.name
34
+ type = type[0..type.size-4]
35
+ if (class_exists?(type, sym))
36
+ type = type.constantize
37
+ control = di_instantiate(type)
38
+ params = extract_params_for(control.method(sym))
39
+ ret = control.send sym, *params
40
+ control.instance_variables.each do |x|
41
+ value = control.instance_variable_get x
42
+ instance_variable_set x, value
43
+ end
44
+ super(sym)
45
+ else
46
+ params = extract_params_for(method(sym))
47
+ super(sym, *params)
48
+ end
49
+ end
50
+
51
+ def di_instantiate(type)
52
+ type.new
53
+ end
54
+
55
+ private
56
+ def extract_params_for(method)
57
+ types = method.parameters
58
+ if has_parameters(types)
59
+ provide_instances_for(types)
60
+ else
61
+ []
62
+ end
63
+ end
64
+
65
+ def has_parameters(types)
66
+ types.size!=0 && types[0]!=[:rest]
67
+ end
68
+
69
+ def provide_instances_for(params)
70
+ params.collect do |param|
71
+ provide(param[1])
72
+ end
73
+ end
74
+
75
+ KEYS = Handler::KeyHandler.new
76
+ PROVIDERS = [KEYS, Handler::SessionLocator.new, Handler::Load.new, Handler::Instantiate.new, Handler::ParamsAndScopes.new, Handler::Param.new]
77
+
78
+ KEYS["redirect_to"] = Helper::RedirectTo
79
+ KEYS["result"] = Helper::Result
80
+
81
+ def provide(p)
82
+ name = p.to_s
83
+ provider = PROVIDERS.find do |f|
84
+ f.handles?(self, name)
85
+ end
86
+ provider ? provider.extract(self, name) : nil
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAndSolid
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ module RailsAndSolid
2
+
3
+ autoload :Helper, 'rails-and-solid/helper'
4
+ autoload :TrickHim, 'rails-and-solid/trick_him'
5
+ autoload :Handler, 'rails-and-solid/handler'
6
+
7
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rails-and-solid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rails-and-solid"
7
+ s.version = RailsAndSolid::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Guilherme Silveira"]
10
+ s.email = ["guilherme.silveira@caelum.com.br"]
11
+ s.homepage = ""
12
+ s.summary = "S.o.l.i.d.ifying 300 methods. An OO approach for a procedural one. Let's see what happens."
13
+ s.description = s.summary
14
+
15
+ s.rubyforge_project = "rails-and-solid"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency("rspec")
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-and-solid
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 0
9
+ version: 0.9.0
10
+ platform: ruby
11
+ authors:
12
+ - Guilherme Silveira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-08 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: S.o.l.i.d.ifying 300 methods. An OO approach for a procedural one. Let's see what happens.
34
+ email:
35
+ - guilherme.silveira@caelum.com.br
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README
46
+ - Rakefile
47
+ - autotest/discover.rb
48
+ - lib/rails-and-solid.rb
49
+ - lib/rails-and-solid/handler.rb
50
+ - lib/rails-and-solid/handler/instantiate.rb
51
+ - lib/rails-and-solid/handler/key_handler.rb
52
+ - lib/rails-and-solid/handler/load.rb
53
+ - lib/rails-and-solid/handler/param.rb
54
+ - lib/rails-and-solid/handler/params_and_scopes.rb
55
+ - lib/rails-and-solid/handler/session_locator.rb
56
+ - lib/rails-and-solid/helper.rb
57
+ - lib/rails-and-solid/helper/redirect_to.rb
58
+ - lib/rails-and-solid/helper/result.rb
59
+ - lib/rails-and-solid/trick_him.rb
60
+ - lib/rails-and-solid/version.rb
61
+ - rails-and-solid.gemspec
62
+ has_rdoc: true
63
+ homepage: ""
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project: rails-and-solid
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: S.o.l.i.d.ifying 300 methods. An OO approach for a procedural one. Let's see what happens.
94
+ test_files: []
95
+