Class: Jcontroller::Jaction
- Inherits:
-
Object
- Object
- Jcontroller::Jaction
- Defined in:
- lib/jcontroller/jaction.rb
Constant Summary collapse
- ATTRIBUTES =
%w[controller_path action_name format params]
Class Method Summary collapse
Instance Method Summary collapse
- #format=(format) ⇒ Object
-
#initialize(jaction = nil) ⇒ Jaction
constructor
A new instance of Jaction.
- #params=(params) ⇒ Object
- #params_template_path ⇒ Object
- #parse(jaction) ⇒ Object
- #state ⇒ Object
- #to_params ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(jaction = nil) ⇒ Jaction
Returns a new instance of Jaction.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/jcontroller/jaction.rb', line 6 def initialize(jaction=nil) if self.class.controller self.controller_path = self.class.controller.controller_path self.action_name = self.class.controller.action_name self.format = self.class.controller.formats.first end self.params = {} if jaction parse(jaction) end end |
Class Method Details
.controller ⇒ Object
82 83 84 |
# File 'lib/jcontroller/jaction.rb', line 82 def controller RequestStore.store[:jaction_controller] end |
.controller=(controller) ⇒ Object
86 87 88 |
# File 'lib/jcontroller/jaction.rb', line 86 def controller=(controller) RequestStore.store[:jaction_controller] = controller end |
Instance Method Details
#format=(format) ⇒ Object
62 63 64 |
# File 'lib/jcontroller/jaction.rb', line 62 def format=(format) @format = format.to_sym end |
#params=(params) ⇒ Object
65 66 67 |
# File 'lib/jcontroller/jaction.rb', line 65 def params=(params) @params = params.class == String ? params : HashWithIndifferentAccess.new(params) end |
#params_template_path ⇒ Object
58 59 60 |
# File 'lib/jcontroller/jaction.rb', line 58 def params_template_path "#{controller_path}/#{action_name}_params" end |
#parse(jaction) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jcontroller/jaction.rb', line 19 def parse(jaction) if jaction.class == Jaction ATTRIBUTES.each do |attribute| self.send(attribute + "=", jaction.send(attribute)) end elsif jaction.class.ancestors.include? Hash hash = HashWithIndifferentAccess.new(jaction) ATTRIBUTES.each do |attribute| if hash[attribute]; self.send(attribute + "=", hash[attribute]) end end else match = jaction.match /(.+(?=\/|#))(?:[\/#](.+?(?=\.|\?|\z))(?:\.(.+?)(?=\?|\z)(?:\?(.+(?=\z)))?)?)?/ if match %w[controller_path action_name format].zip((1..3)).each do |attribute, match_index| value = match[match_index] if value; send(attribute + "=", value) end end self.params = Rack::Utils.parse_nested_query(match[4]) if params[:jaction_params]; self.params = params[:jaction_params] end else self.action_name = jaction end end end |
#state ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/jcontroller/jaction.rb', line 48 def state self.class.controller.send(:jcontroller_state).merge({ jaction: { controller_path: controller_path, action_name: action_name, format: format } }) end |
#to_params ⇒ Object
44 45 46 |
# File 'lib/jcontroller/jaction.rb', line 44 def to_params [state.to_json, string_params].join(",") end |
#to_s ⇒ Object
69 70 71 72 73 74 |
# File 'lib/jcontroller/jaction.rb', line 69 def to_s s = "#{controller_path}/#{action_name}" if format; s += ".#{format}" end unless params.blank?; s += "?#{ (params.class == String ? { :jaction_params => params } : params).to_query }" end s end |