Module: Pepper::Context::InstanceMethods

Included in:
Base, Delegate::ClassMethods
Defined in:
lib/pepper.rb

Constant Summary collapse

HTTP_METHODS =

Create the HTTP action methods…

%W(get post put delete)
PATTERNS =
{
  :digit=>/^\d+$/,
  :word=>/^\w+$/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



82
83
84
# File 'lib/pepper.rb', line 82

def method_missing(m,*args,&block)
  @app.send(m, *args, &block) if [:header, :headers].include?(m)
end

Instance Attribute Details

#appObject

the base application instance



59
60
61
# File 'lib/pepper.rb', line 59

def app
  @app
end

#blockObject (readonly)

the block that holds the executable context code



80
81
82
# File 'lib/pepper.rb', line 80

def block
  @block
end

#delegateObject (readonly)

optional module name to delegate logic for a given sub-path



78
79
80
# File 'lib/pepper.rb', line 78

def delegate
  @delegate
end

#optionsObject (readonly)

the options passed into map method calls



76
77
78
# File 'lib/pepper.rb', line 76

def options
  @options
end

#parentObject (readonly)

The parent context



74
75
76
# File 'lib/pepper.rb', line 74

def parent
  @parent
end

#patternObject (readonly)

attributes that are set by the “initialize” method

can be a string, regexp, hash or symbol

* string - the current path slice will be compared using ==
* regexp - the current path slice will be compared using =~
* hash - if the value matches, the current slice is assigned to the request.params using the key
  * the value can in-turn be any string, regexp, hash or symbol
* symbol - can be used to match preset rules found in the +PATTERNS+ hash
  if the symbol is not found in +PATTERNS+, it's converted to a string and uses ==


72
73
74
# File 'lib/pepper.rb', line 72

def pattern
  @pattern
end

Instance Method Details

#actionsObject



118
# File 'lib/pepper.rb', line 118

def actions; @actions||=[] end

#after(action = :all, &block) ⇒ Object



128
129
130
# File 'lib/pepper.rb', line 128

def after(action=:all, &block)
  after_blocks[action]=block
end

#after_blocksObject



122
# File 'lib/pepper.rb', line 122

def after_blocks; @after_blocks||={} end

#before(action = :all, &block) ⇒ Object



124
125
126
# File 'lib/pepper.rb', line 124

def before(action=:all, &block)
  before_blocks[action]=block
end

#before_blocksObject



120
# File 'lib/pepper.rb', line 120

def before_blocks; @before_blocks||={} end

#child_exists?(pattern, options) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/pepper.rb', line 211

def child_exists?(pattern, options)
  children.detect{|child|child.pattern==pattern and child.options==options}
end

#childrenObject

returns the child contexts



108
# File 'lib/pepper.rb', line 108

def children; @children||=[]; end

#delegate_to(handler) ⇒ Object

Shortcut assigning a delegate



206
207
208
209
# File 'lib/pepper.rb', line 206

def delegate_to(handler)
  @delegate=handler
  throw :delegate
end

#find_action(method, env = {}) ⇒ Object

Finds the action (get, post, put etc.) base on:

  • the method

  • the env/hash values



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pepper.rb', line 137

def find_action(method, env={})
  actions.each do |a|
    next if a.method != method
    return a if a.options.nil? or a.options.size==0
    a.options.each_pair do |k,v|
      k=k.to_s.upcase
      return a if env[k]==v or env[k]=~v
    end
  end
  nil
end

#map(pattern, options = {}, delegate = nil, &block) ⇒ Object

The main method for creating child contexts



218
219
220
221
222
223
224
225
226
227
# File 'lib/pepper.rb', line 218

def map(pattern, options={}, delegate=nil, &block)
  if child=child_exists?(pattern, options)
    # refresh the app
    child.app=@app
    return child
  else
    children << Context::Base.new(pattern, options, delegate, self, &block)
    children.last.app=app
  end
end

#match?(pattern, value) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/pepper.rb', line 157

def match?(pattern, value)
  ok = simple_match?(pattern, value)
  return ok if ok
  # if hash, use the value as the pattern and set the request param[key] to the value argument
  return (params[pattern.keys.first] = match?(pattern[pattern.keys.first], value)) if pattern.is_a?(Hash)
  # if symbol
  if pattern.is_a?(Symbol)
    # lookup the symbol in the PATTERNS hash
    return value if PATTERNS[pattern] and match?(PATTERNS[pattern], value)
    # There was no match, convert to string
    simple_match?(pattern.to_s, value)
  end
end

#paramsObject

shortcut for request.params



116
# File 'lib/pepper.rb', line 116

def params; request.params; end

#requestObject

returns instance of Rack::Request



114
# File 'lib/pepper.rb', line 114

def request; app.request; end

#resolve(slices) ⇒ Object

Recursively looks for matching context instances returning the last one that matches.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/pepper.rb', line 184

def resolve(slices)
  return unless match?(pattern, slices.first)
  @value=slices.shift
  catch :delegate do
    instance_eval(&@block) if @block
  end
  if delegate
    raise 'Only module based delegates are allowed' unless delegate.class==Module
    return delegate.delegate!(self, [@value]+slices)
  end
  return self if slices.size==0
  children.each do |child|
    if found=child.resolve(slices)
      return found
    end
  end
  nil
end

#simple_match?(pattern, value) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
# File 'lib/pepper.rb', line 174

def simple_match?(pattern, value)
  # if string, simple comparison
  return value if (pattern.is_a?(String) and value == pattern)
  # if regexp, regx comparison
  return value if (pattern.is_a?(Regexp) and value =~ pattern)
end

#valueObject

returns the value from the path slices that this context matched



110
# File 'lib/pepper.rb', line 110

def value; @value||=nil; end