Class: CanDo::Dsl
- Inherits:
-
Object
- Object
- CanDo::Dsl
- Defined in:
- lib/can_do/dsl.rb
Defined Under Namespace
Constant Summary collapse
- NIL_REASON =
"Object of query does not exist."
Instance Method Summary collapse
- #can(action, noun, &block) ⇒ Object
- #can?(action, object) ⇒ Boolean
- #cascade(action, &block) ⇒ Object
-
#initialize(&block) ⇒ Dsl
constructor
A new instance of Dsl.
- #internal_reason(action, noun, object) ⇒ Object
- #noun(object) ⇒ Object
- #reason(action, object) ⇒ Object
- #rule(reason = nil, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Dsl
Returns a new instance of Dsl.
43 44 45 46 |
# File 'lib/can_do/dsl.rb', line 43 def initialize &block @can_hash = {} instance_eval &block end |
Instance Method Details
#can(action, noun, &block) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/can_do/dsl.rb', line 28 def can(action, noun, &block) @rules = [] yield @can_hash[action] ||= {} @can_hash[action][noun] = @rules end |
#can?(action, object) ⇒ Boolean
9 10 11 12 13 |
# File 'lib/can_do/dsl.rb', line 9 def can?(action, object) can = internal_reason(action, noun(object), object).nil? yield if can && block_given? can end |
#cascade(action, &block) ⇒ Object
39 40 41 |
# File 'lib/can_do/dsl.rb', line 39 def cascade(action, &block) @rules << Cascade.new(action, self, &block) end |
#internal_reason(action, noun, object) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/can_do/dsl.rb', line 19 def internal_reason(action, noun, object) return NIL_REASON if object.nil? rules = @can_hash[action][noun] rules.each do |rule| return rule.reason unless rule.call(object, noun) end nil end |
#noun(object) ⇒ Object
4 5 6 7 |
# File 'lib/can_do/dsl.rb', line 4 def noun(object) noun = object.class unless object.class == Class || object.class == String noun || object end |
#reason(action, object) ⇒ Object
15 16 17 |
# File 'lib/can_do/dsl.rb', line 15 def reason(action, object) internal_reason(action, noun(object), object) end |