Class: SelfControl::Action
- Inherits:
-
Object
- Object
- SelfControl::Action
- Defined in:
- lib/self-control/action.rb
Instance Attribute Summary collapse
-
#for ⇒ Object
Returns the value of attribute for.
-
#goto ⇒ Object
Returns the value of attribute goto.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #in_context(option, model, env) ⇒ Object
-
#initialize(name, options = {}) ⇒ Action
constructor
A new instance of Action.
- #url_from(model, env) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Action
Returns a new instance of Action.
5 6 7 8 9 10 |
# File 'lib/self-control/action.rb', line 5 def initialize(name, ={}) @name = name @goto = .delete(:goto) @hash = .delete(:hash) @arguments = .delete(:the) || .delete(:for) || .delete(:with) end |
Instance Attribute Details
#for ⇒ Object
Returns the value of attribute for.
3 4 5 |
# File 'lib/self-control/action.rb', line 3 def for @for end |
#goto ⇒ Object
Returns the value of attribute goto.
3 4 5 |
# File 'lib/self-control/action.rb', line 3 def goto @goto end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/self-control/action.rb', line 3 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/self-control/action.rb', line 3 def @options end |
Instance Method Details
#in_context(option, model, env) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/self-control/action.rb', line 27 def in_context(option, model, env) case option when String then option when Symbol if model.respond_to?(option) then model.send(option) elsif option.to_s.starts_with?('@') env.instance_variable_get(option) else option end when Array option.map { |opt| in_context(opt, model, env) } when Hash Hash[option.map { |k,v| [k.to_sym, in_context(v, model, env)] }] when Proc option.call(*[model,env].take(option.arity >= 0 ? option.arity : 0)) end end |
#url_from(model, env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/self-control/action.rb', line 12 def url_from(model, env) goto = in_context(@goto, model, env) hash = in_context(@hash, model, env) args = in_context(@arguments, model, env) url = if goto.is_a?(String) then goto elsif goto.is_a?(Symbol) env.send(goto, args) else env.send(:url_for, (goto || args)) end hash ? "#{url}##{hash}" : url end |