rpbk 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. data/lib/rpbk/ext/renderer.rb +26 -0
  2. data/lib/rpbk/holder.rb +151 -0
  3. data/lib/rpbk/set.rb +77 -0
  4. data/lib/rpbk/version.rb +1 -1
  5. data/test/dummy/.rspec +1 -0
  6. data/test/dummy/Rakefile +7 -0
  7. data/test/dummy/app/assets/javascripts/admin/controller1.js +2 -0
  8. data/test/dummy/app/assets/javascripts/application.js +9 -0
  9. data/test/dummy/app/assets/javascripts/controller1.js +2 -0
  10. data/test/dummy/app/assets/stylesheets/admin/controller1.css +4 -0
  11. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  12. data/test/dummy/app/assets/stylesheets/controller1.css +4 -0
  13. data/test/dummy/app/controllers/admin/controller1_controller.rb +5 -0
  14. data/test/dummy/app/controllers/application_controller.rb +3 -0
  15. data/test/dummy/app/controllers/controller1_controller.rb +8 -0
  16. data/test/dummy/app/helpers/admin/controller1_helper.rb +2 -0
  17. data/test/dummy/app/helpers/application_helper.rb +2 -0
  18. data/test/dummy/app/helpers/controller1_helper.rb +2 -0
  19. data/test/dummy/app/mailers/.gitkeep +0 -0
  20. data/test/dummy/app/models/.gitkeep +0 -0
  21. data/test/dummy/app/views/admin/controller1/action1.html.erb +1 -0
  22. data/test/dummy/app/views/controller1/action1.html.erb +1 -0
  23. data/test/dummy/app/views/controller1/action2.html.erb +2 -0
  24. data/test/dummy/app/views/layouts/_test_partial.html.erb +1 -0
  25. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  26. data/test/dummy/app/views/layouts/web.html.erb +15 -0
  27. data/test/dummy/config.ru +4 -0
  28. data/test/dummy/config/application.rb +45 -0
  29. data/test/dummy/config/boot.rb +10 -0
  30. data/test/dummy/config/database.yml +25 -0
  31. data/test/dummy/config/environment.rb +5 -0
  32. data/test/dummy/config/environments/development.rb +30 -0
  33. data/test/dummy/config/environments/production.rb +60 -0
  34. data/test/dummy/config/environments/test.rb +42 -0
  35. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/dummy/config/initializers/inflections.rb +10 -0
  37. data/test/dummy/config/initializers/mime_types.rb +5 -0
  38. data/test/dummy/config/initializers/secret_token.rb +7 -0
  39. data/test/dummy/config/initializers/session_store.rb +8 -0
  40. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/test/dummy/config/locales/en.yml +5 -0
  42. data/test/dummy/config/routes.rb +70 -0
  43. data/test/dummy/lib/assets/.gitkeep +0 -0
  44. data/test/dummy/public/404.html +26 -0
  45. data/test/dummy/public/422.html +26 -0
  46. data/test/dummy/public/500.html +26 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/dummy/spec/controllers/admin/controller1_controller_spec.rb +10 -0
  50. data/test/dummy/spec/rpbk/set_spec.rb +350 -0
  51. data/test/dummy/spec/rpbk_spec.rb +25 -0
  52. data/test/dummy/spec/spec_helper.rb +27 -0
  53. data/test/dummy/spec/views/admin/controller1/action1.html.erb_spec.rb +47 -0
  54. data/test/dummy/spec/views/controller1/action1.html.erb_spec.rb +47 -0
  55. data/test/dummy/spec/views/controller1/action2.html.erb_spec.rb +4 -0
  56. metadata +55 -1
@@ -0,0 +1,26 @@
1
+ require 'action_controller/metal/renderers'
2
+ require 'action_controller/metal/responder'
3
+ require 'action_dispatch/http/mime_type'
4
+
5
+ # this will let us do thing like 'render :key => :key_to_partial' in views
6
+ #TODO need to extend the ActionView::Renderer.new.render method not rewrite
7
+ module ActionView
8
+ class Renderer
9
+ def render context, options
10
+ if options[:key]
11
+ if attribute = Rpbk.find_with_context(options[:key], context)
12
+ options[:partial] = attribute.value
13
+ else
14
+ options[:text] = "Partial #{options[:key].inspect} not found!"
15
+ end
16
+ options.delete(:key)
17
+ end
18
+ # copy from : /actionpack-3.1.0/lib/action_view/renderer/renderer.rb function render(context, options)
19
+ if options.key?(:partial)
20
+ render_partial(context, options)
21
+ else
22
+ render_template(context, options)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,151 @@
1
+ module Rpbk
2
+ class Holder
3
+ def initialize
4
+ @main_level = Level.new 'main_level'
5
+ @levels = [@main_level]
6
+ @controllers = {}
7
+ @groups = {}
8
+ @layout = nil
9
+ end
10
+
11
+ def extend_from names
12
+ names ||= []
13
+ names = [names] if names.is_a?(String) || names.is_a?(Symbol)
14
+ names.each do |name|
15
+ name = name.to_sym
16
+ group = find_action(@levels.last.controller_name, name) if @levels.last.group_type == :action
17
+ @levels.last.extend_from(group || find_controller(name) || find_group(name))
18
+ end
19
+ end
20
+
21
+ def layout
22
+ @main_level.layout
23
+ end
24
+
25
+ def set_layout name
26
+ @levels.last.layout = name
27
+ end
28
+
29
+ def scope
30
+ @levels.last
31
+ end
32
+
33
+ def set_scope type, name
34
+ case type
35
+ when :controller
36
+ @controllers[name] = Controller.new name unless find_controller(name)
37
+ @levels << @controllers[name]
38
+ when :action
39
+ raise ActionControllerNotSpecifiedException.new "#{name} should have an controller" unless @levels.last && @levels.last.group_type == :controller
40
+
41
+ @controllers[@levels.last.name].add_action(name) unless find_action(@levels.last.name, name)
42
+ @levels << find_action(@levels.last.name, name)
43
+ when :group
44
+ @groups[name] = Group.new name unless find_group(name)
45
+ @levels << @groups[name]
46
+ end
47
+ end
48
+
49
+ def drop_last_scope
50
+ @levels.pop
51
+ end
52
+
53
+ def add_attr attribute
54
+ @levels.last.add_attr attribute
55
+ end
56
+
57
+ def find_group name
58
+ @groups[name]
59
+ end
60
+
61
+ def find_attr name
62
+ @main_level.find_attr(name)
63
+ end
64
+
65
+ def find_action controller_name, name
66
+ @controllers[controller_name].find_action(name) if @controllers[controller_name]
67
+ end
68
+
69
+ def find_controller name
70
+ @controllers[name]
71
+ end
72
+ end
73
+
74
+ class Level
75
+ attr_accessor :layout
76
+ def initialize name
77
+ @group_type, @attributes = :main, {}
78
+ end
79
+
80
+ def group_type
81
+ @group_type
82
+ end
83
+
84
+ def add_attr attribute
85
+ @attributes[attribute.key] = attribute
86
+ end
87
+
88
+ def find_attr name
89
+ @attributes[name]
90
+ end
91
+
92
+ def attributes
93
+ @attributes
94
+ end
95
+
96
+ def extend_from group
97
+ if group
98
+ @attributes.merge! group.attributes
99
+ @layout = group.layout
100
+ end
101
+ end
102
+ end
103
+
104
+ class Group < Level
105
+ attr_accessor :name
106
+ def initialize name
107
+ super name
108
+ @group_type, @name = :group, name
109
+ end
110
+ end
111
+
112
+ class Controller < Level
113
+ attr_accessor :name
114
+ def initialize name
115
+ super name
116
+ @group_type, @name, @actions = :controller, name, {}
117
+ end
118
+
119
+ def add_action name
120
+ @actions[name] = Action.new name, @name
121
+ end
122
+
123
+ def find_action name
124
+ @actions[name]
125
+ end
126
+
127
+ end
128
+
129
+ class Action < Level
130
+ attr_accessor :name
131
+ attr_reader :controller_name
132
+ def initialize name, controller_name
133
+ super name
134
+ @controller_name, @name, @group_type = controller_name, name, :action
135
+ end
136
+
137
+ end
138
+
139
+ class Attribute
140
+ attr_accessor :key, :value, :type
141
+
142
+ def initialize key, value
143
+ @type, @key, @value = :attribute, key, value
144
+ end
145
+
146
+ def equal? action
147
+ action.class == Attribute && action.key == self.key && action.value && self.value
148
+ end
149
+ end
150
+
151
+ end
@@ -0,0 +1,77 @@
1
+ module Rpbk
2
+ class Set
3
+ attr_accessor :groups, :controllers, :name
4
+ def initialize
5
+ @holder = Holder.new
6
+ @data = {}
7
+ end
8
+
9
+ def find key, controller_name = '', action_name = ''
10
+ ret = @holder.find_attr(key)
11
+ if controller = @holder.find_controller(controller_name)
12
+ ret = controller.find_attr(key) || ret
13
+ end
14
+ if action = @holder.find_action(controller_name, action_name)
15
+ ret = action.find_attr(key) || ret
16
+ end
17
+ ret
18
+ end
19
+
20
+ def find_layout controller_name = '', action_name = ''
21
+ ret = @holder.layout
22
+ if controller = @holder.find_controller(controller_name)
23
+ ret = controller.layout || ret
24
+ end
25
+ if action = @holder.find_action(controller_name, action_name)
26
+ ret = action.layout || ret
27
+ end
28
+ ret
29
+ end
30
+
31
+ def find_in_group key, group_name
32
+ if group = @holder.find_group(group_name)
33
+ return group.find_attr(key)
34
+ end
35
+ nil
36
+ end
37
+
38
+ def config(&block)
39
+ instance_exec(&block)
40
+ nil
41
+ end
42
+
43
+ def set attributes
44
+ attributes.kind_of? Hash and attributes.each do |key, value|
45
+ @holder.add_attr Attribute.new(key, value)
46
+ end
47
+ end
48
+
49
+ def group name, options = {}, &block
50
+ process_group :group, name, options, &block
51
+ end
52
+
53
+ def controller name, options = {}, &block
54
+ process_group :controller, name, options, &block
55
+ end
56
+
57
+ def action name, options ={}, &block
58
+ process_group :action, name, options, &block
59
+ end
60
+
61
+ def layout name
62
+ @holder.set_layout name.to_sym
63
+ end
64
+
65
+ private
66
+
67
+ def process_group type, name, options, &block
68
+ @holder.set_scope type, name.to_sym
69
+ @holder.extend_from(options[:extend]) if options[:extend]
70
+ yield if block_given?
71
+ @holder.drop_last_scope
72
+ end
73
+ end
74
+
75
+ class ActionControllerNotSpecifiedException < Exception
76
+ end
77
+ end
@@ -1,3 +1,3 @@
1
1
  module Rpbk
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ class Admin::Controller1Controller < ApplicationController
2
+ def action1
3
+ end
4
+
5
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,8 @@
1
+ class Controller1Controller < ApplicationController
2
+ def action1
3
+ end
4
+
5
+ def action2
6
+ end
7
+
8
+ end
@@ -0,0 +1,2 @@
1
+ module Admin::Controller1Helper
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module Controller1Helper
2
+ end
File without changes
File without changes
@@ -0,0 +1 @@
1
+ <%= render :key => :test_partial %>
@@ -0,0 +1 @@
1
+ <%= render :key => :test_partial %>
@@ -0,0 +1,2 @@
1
+ <h1>Controller1#action2</h1>
2
+ <p>Find me in app/views/controller1/action2.html.erb</p>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <h1>Web layout!!!</h1>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+ require "rpbk"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # Configure the default encoding used in templates for Ruby 1.9.
33
+ config.encoding = "utf-8"
34
+
35
+ # Configure sensitive parameters which will be filtered from the log file.
36
+ config.filter_parameters += [:password]
37
+
38
+ # Enable the asset pipeline
39
+ config.assets.enabled = true
40
+
41
+ # Version of your assets, change this if you want to expire all your assets
42
+ config.assets.version = '1.0'
43
+ end
44
+ end
45
+