Class: Platanus::Canned2::UponContext

Inherits:
Object
  • Object
show all
Defined in:
lib/platanus/canned2.rb

Overview

Upon block context. allows ” do

upon(:user_data) { matches(:site_id, using: :equals_int) or matches(:section_id) and passes(:is_owner) }
upon { matches('current_user.site_id', with: :site_id) or matches(:section_id) }
upon(:user) { matches(:site_id) or matches(:section_id) and passes(:test) or holds('user.is_active?') }
upon { holds('@raffle.id == current_user.id') }

end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_res, _ctx, _tests, _def_matcher) ⇒ UponContext

Returns a new instance of UponContext.



191
192
193
194
195
196
# File 'lib/platanus/canned2.rb', line 191

def initialize(_res, _ctx, _tests, _def_matcher)
  @res = _res
  @ctx = _ctx
  @tests = _tests
  @def_matcher = _def_matcher
end

Class Method Details

.load_value_for(_ctx, _key_or_expr) ⇒ Object



184
185
186
187
188
189
# File 'lib/platanus/canned2.rb', line 184

def self.load_value_for(_ctx, _key_or_expr)
  return _ctx if _key_or_expr.nil?
  return _ctx[_key_or_expr] if _ctx.is_a? Hash
  return _ctx.send(_key_or_expr) if _key_or_expr.is_a? Symbol
  return _ctx.instance_eval(_key_or_expr)
end

Instance Method Details

#action_is(*_actions) ⇒ Object

Test whether the current action matches a list of actions



199
200
201
# File 'lib/platanus/canned2.rb', line 199

def action_is(*_actions)
  _actions.any? { |a| a.to_s == @ctx.action_name }
end

#action_is_not(*_actions) ⇒ Object

Test whether the current action is no in list of actions



204
205
206
# File 'lib/platanus/canned2.rb', line 204

def action_is_not(*_actions)
  !_actions.any? { |a| a.to_s == @ctx.action_name }
end

#holds(_what) ⇒ Object

Tests whether a given expression evaluated in the resource context returns true.

IMPORTANT if no resource is provided the current controller instance is used instead.

Parameters:

  • _what (Symbol|String)

    if symbol, then send is used to call a context’s function with that name, if a string, then instance_eval is used to evaluate it.



276
277
278
# File 'lib/platanus/canned2.rb', line 276

def holds(_what)
  _what.is_a? Symbol ? @res.send(_what) : @res.instance_eval(_what.to_s)
end

#passes(_test = nil, _options = {}, &_block) ⇒ Object Also known as: checks

Test whether the current resource passes a given test.

IMPORTANT Tests are executed in the current controller context.

Parameters:

  • _test (Symbol) (defaults to: nil)

    test identifier.

  • :on (Symbol|String)

    optional key or expression used to retrieve from the resource the value to be passed to the test instead of the resource.

  • _block (Block)

    block to be executed (if test identifier is not given)



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/platanus/canned2.rb', line 253

def passes(_test=nil, _options={}, &_block)

  if !_test.nil?
    test = @tests[_test]
    raise SetupError.new "Invalid test identifier '#{_test}'" if test.nil?
  elsif !_block.nil?
    test = _block
    raise SetupError.new "Invalid block arity" if _block.arity > 1
  else raise SetupError.new "Must provide a test name or a block" end

  if test.arity == 1
    user_value = self.class.load_value_for(@res, _options[:on])
    @ctx.instance_exec(user_value, &test)
  else @ctx.instance_eval &test end
end

#same(_what, _options = {}) ⇒ Object

Tests for a match between one of the request’s parameters and a resource expression.

IMPORTANT if no resource is provided the current controller instance is used instead.

Parameters:

  • _what (Symbol)

    parameter name.

  • :using (Symbol)

    matcher (:equals|:equals_int|:higher_than|:lower_than), uses profile default matcher if not provided.

  • :key (Symbol|String)

    key or expression used to retrieve the matching value for current resource, if not given then _what is used.

  • :value (Mixed)

    if given, this value is matched against parameter instead of resource’s.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/platanus/canned2.rb', line 219

def same(_what, _options={})
  matcher = _options.fetch(:using, @def_matcher)

  param = @ctx.params[_what]
  return (matcher == :nil) if param.nil? # :nil matcher

  if _options.has_key? :value
    user_value = _options[:value]
  else
    user_value = self.class.load_value_for(@res, _options.fetch(:key, _what))
    return false if user_value.nil?
    return true if user_value == :wildcard
  end

  case matcher
  when :equals; user_value == param
  when :equals_int; user_value.to_i == param.to_i
  when :higher_than; param > user_value
  when :lower_than; param < user_value
  else
    # TODO: use custom matcher.
    false
  end
end