Class: ActionFlow::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/action_flow/expression.rb

Defined Under Namespace

Modules: Generator Classes: Group

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, params, &block) ⇒ Expression

Returns a new instance of Expression.



4
5
6
7
8
9
10
# File 'lib/action_flow/expression.rb', line 4

def initialize(controller, params, &block)
  @params     = params.dup
  @controller = (@params.delete(:controller) || controller).to_s
  @action     = @params.delete(:action).to_s if @params[:action]
  @format     = @params.delete(:format).to_s if @params[:format]
  @matcher    = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, params = {}, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/action_flow/expression.rb', line 35

def method_missing(name, params = {}, &block)
  @format = name.to_s if @action
  @action ||= name.to_s
  @params.update(params) if Hash === params
  @matcher = block if block_given?
  self
end

Class Method Details

.from_hash(hash) ⇒ Object



12
13
14
# File 'lib/action_flow/expression.rb', line 12

def self.from_hash(hash)
  new(nil, hash)
end

Instance Method Details

#*(expression) ⇒ Object



20
21
22
23
24
# File 'lib/action_flow/expression.rb', line 20

def *(expression)
  @format = expression.instance_eval { @controller }
  @action = nil
  self
end

#+(expression) ⇒ Object



16
17
18
# File 'lib/action_flow/expression.rb', line 16

def +(expression)
  self.class::Group.new + self + expression
end

#/(expression) ⇒ Object



26
27
28
29
# File 'lib/action_flow/expression.rb', line 26

def /(expression)
  expression.nesting = @controller
  expression
end

#===(context) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/action_flow/expression.rb', line 47

def ===(context)
  p, req = context.params, context.request
  
  return false if (@controller != p[:controller].to_s)     or
                  (@action and @action != p[:action].to_s) or
                  (@verb and !req.__send__("#{ @verb }?")) or
                  (@format and @format != p[:format].to_s)
  
  return false unless @params.all? do |key, value|
    Variable === value ||
    (value == p[key]) || (value == p[key].to_s) ||
    (value === p[key]) || (value === p[key].to_i)
  end
  
  @matcher ? context.instance_eval(&@matcher) : true
end

#inspectObject



77
78
79
80
81
82
83
84
# File 'lib/action_flow/expression.rb', line 77

def inspect
  source = @controller.dup
  source << ".#{ @action }" if @action
  source << "(#{ @params.map { |k,v| ":#{k} => #{v.inspect}" } * ', ' })" unless @params.empty?
  source << "#{ @action ? '.' : '*' }#{ @format }" if @format
  source = "#{ @verb }(#{ source })" if @verb
  source
end

#nesting=(name) ⇒ Object



31
32
33
# File 'lib/action_flow/expression.rb', line 31

def nesting=(name)
  @controller = "#{ name }/#{ @controller }"
end

#to_params(env = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/action_flow/expression.rb', line 64

def to_params(env = {})
  options = {:controller => '/' + @controller}
  options[:action] = @action || :index
  options[:format] = @format if @format
  
  @params.each do |key, value|
    value = value.lookup(env) if Variable === value
    options[key] = value
  end
  
  options
end

#verb=(verb) ⇒ Object



43
44
45
# File 'lib/action_flow/expression.rb', line 43

def verb=(verb)
  @verb = verb.to_s
end