Class: SelfControl::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/self-control/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @name = name
  @goto = options.delete(:goto)
  @hash = options.delete(:hash)
  @arguments = options.delete(:the) || options.delete(:for) || options.delete(:with)
end

Instance Attribute Details

#forObject

Returns the value of attribute for.



3
4
5
# File 'lib/self-control/action.rb', line 3

def for
  @for
end

#gotoObject

Returns the value of attribute goto.



3
4
5
# File 'lib/self-control/action.rb', line 3

def goto
  @goto
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/self-control/action.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/self-control/action.rb', line 3

def options
  @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