Module: Platanus::Canned2::Definition::ClassMethods

Defined in:
lib/platanus/canned2.rb

Constant Summary collapse

@@tests =
{}
@@profiles =
{}

Instance Method Summary collapse

Instance Method Details

#can?(_ctx, _profile, _action) ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
334
335
336
337
338
339
340
# File 'lib/platanus/canned2.rb', line 331

def can?(_ctx, _profile, _action)
  profile = @@profiles[_profile.to_s]
  return if profile.nil?
  profile.rules[_action].any? do |rule|
    next true if rule.nil?
    rule_ctx = RuleContext.new _ctx, @@tests, profile.def_matcher, profile.def_resource
    rule_ctx.instance_eval(&rule)
    rule_ctx.passed?
  end
end

#profile(_name, _options = {}, &_block) ⇒ Object

Creates a new profile and evaluates the given block using the profile context.

Parameters:

  • _name (String|Symbol)

    Profile name.

  • :inherits (String|Symbol)

    Name of profile to inherit rules from.

  • :matcher (Symbol)

    Default matcher for matches tests

  • :resource (Symbol)

    Default resource for upon expressions



321
322
323
324
325
326
327
328
# File 'lib/platanus/canned2.rb', line 321

def profile(_name, _options={}, &_block)
  profile = @@profiles[_name.to_s] = Profile.new(
    @@profiles[_options.fetch(:inherits, nil).to_s],
    _options.fetch(:matcher, :equals),
    _options.fetch(:resource, nil)
  )
  profile.instance_eval &_block
end

#test(_name, &_block) ⇒ Object

Defines a new test that can be used in “certifies” instructions

IMPORTANT Tests are executed in the controller’s context and passed the tested resource as parameter (only if arity == 1)

Parameters:

  • _name (Symbol)

    test identifier

  • _block (Block)

    test block



308
309
310
311
312
# File 'lib/platanus/canned2.rb', line 308

def test(_name, &_block)
  raise SetupError.new "Invalid test arity for '#{_name}'" if _block.arity > 1
  raise SetupError.new "Duplicated test identifier" if @@tests.has_key? _name
  @@tests[_name] = _block
end