Module: SimpleRules

Extended by:
Configuration
Defined in:
lib/simple_rules.rb,
lib/simple_rules/rule.rb,
lib/simple_rules/version.rb,
lib/simple_rules/configuration.rb

Defined Under Namespace

Modules: Configuration Classes: NotAuthorized, Rule, RuleNotFindError, SimpleRulesError

Constant Summary collapse

VERSION =
'0.0.1'
@@simple_rules =
Array.new

Constants included from Configuration

Configuration::OPTION_NAMES

Class Method Summary collapse

Methods included from Configuration

configure

Class Method Details

.can(action, object, error_message: nil, &block) ⇒ Object

Examples:

SimpleRules.can :some_action, SomeObject do |object, subject|
  subject.some_attr ==  object.some_attr
end


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

def can(action, object, error_message: nil, &block)
  @@simple_rules << Rule.new(action, object.to_s, error_message: error_message, &block)
end

.can?(action, object, subject) ⇒ Boolean

Examples:

SimpleRules.can? :some_action, some_object, subject


Raises:

  • NotAuthorized



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_rules.rb', line 16

def can?(action, object, subject)
  if object.class == Class
    klass = object.to_s.split(':').last
  else
    klass = object.class.to_s.split(':').last
  end

  rule = self.get_rule(action, klass)

  if (rule.block.call object, subject)
    true
  else
    if raise_not_authorized
      raise NotAuthorized, rule.error_message
    else
      false
    end
  end
end