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
-
#app ⇒ Object
the base application instance.
-
#block ⇒ Object
readonly
the block that holds the executable context code.
-
#delegate ⇒ Object
readonly
optional module name to delegate logic for a given sub-path.
-
#options ⇒ Object
readonly
the options passed into
mapmethod calls. -
#parent ⇒ Object
readonly
The parent context.
-
#pattern ⇒ Object
readonly
attributes that are set by the “initialize” method.
Instance Method Summary collapse
- #actions ⇒ Object
- #after(action = :all, &block) ⇒ Object
- #after_blocks ⇒ Object
- #before(action = :all, &block) ⇒ Object
- #before_blocks ⇒ Object
- #child_exists?(pattern, options) ⇒ Boolean
-
#children ⇒ Object
returns the child contexts.
-
#delegate_to(handler) ⇒ Object
Shortcut assigning a delegate.
-
#find_action(method, env = {}) ⇒ Object
Finds the action (get, post, put etc.) base on: * the method * the env/hash values.
-
#map(pattern, options = {}, delegate = nil, &block) ⇒ Object
The main method for creating child contexts.
- #match?(pattern, value) ⇒ Boolean
- #method_missing(m, *args, &block) ⇒ Object
-
#params ⇒ Object
shortcut for request.params.
-
#request ⇒ Object
returns instance of Rack::Request.
-
#resolve(slices) ⇒ Object
Recursively looks for matching context instances returning the last one that matches.
- #simple_match?(pattern, value) ⇒ Boolean
-
#value ⇒ Object
returns the value from the path slices that this context matched.
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
#app ⇒ Object
the base application instance
59 60 61 |
# File 'lib/pepper.rb', line 59 def app @app end |
#block ⇒ Object (readonly)
the block that holds the executable context code
80 81 82 |
# File 'lib/pepper.rb', line 80 def block @block end |
#delegate ⇒ Object (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 |
#options ⇒ Object (readonly)
the options passed into map method calls
76 77 78 |
# File 'lib/pepper.rb', line 76 def @options end |
#parent ⇒ Object (readonly)
The parent context
74 75 76 |
# File 'lib/pepper.rb', line 74 def parent @parent end |
#pattern ⇒ Object (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
#actions ⇒ Object
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_blocks ⇒ Object
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_blocks ⇒ Object
120 |
# File 'lib/pepper.rb', line 120 def before_blocks; @before_blocks||={} end |
#child_exists?(pattern, options) ⇒ Boolean
211 212 213 |
# File 'lib/pepper.rb', line 211 def child_exists?(pattern, ) children.detect{|child|child.pattern==pattern and child.==} end |
#children ⇒ Object
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..nil? or a..size==0 a..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, ={}, delegate=nil, &block) if child=child_exists?(pattern, ) # refresh the app child.app=@app return child else children << Context::Base.new(pattern, , delegate, self, &block) children.last.app=app end end |
#match?(pattern, value) ⇒ 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 |
#params ⇒ Object
shortcut for request.params
116 |
# File 'lib/pepper.rb', line 116 def params; request.params; end |
#request ⇒ Object
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
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 |
#value ⇒ Object
returns the value from the path slices that this context matched
110 |
# File 'lib/pepper.rb', line 110 def value; @value||=nil; end |