Class: Miau::PolicyStorage
- Inherits:
-
Object
- Object
- Miau::PolicyStorage
- Includes:
- Singleton
- Defined in:
- lib/miau/storage.rb
Instance Attribute Summary collapse
-
#instances ⇒ Object
readonly
{ posts: PostsPolicy.new }.
-
#policies ⇒ Object
readonly
Example @policies: { posts: { delete: :delete, remove: :delete }, application: { admin: :check } }.
Instance Method Summary collapse
- #add(klass, action, meth) ⇒ Object
-
#find_policy(klass, action) ⇒ Object
returns policy: [instance, method].
-
#initialize ⇒ PolicyStorage
constructor
A new instance of PolicyStorage.
- #reset ⇒ Object
- #run(klass, action, user, resource) ⇒ Object
- #to_yaml ⇒ Object
Constructor Details
#initialize ⇒ PolicyStorage
Returns a new instance of PolicyStorage.
22 23 24 |
# File 'lib/miau/storage.rb', line 22 def initialize reset end |
Instance Attribute Details
#instances ⇒ Object (readonly)
{ posts: PostsPolicy.new }
20 21 22 |
# File 'lib/miau/storage.rb', line 20 def instances @instances end |
#policies ⇒ Object (readonly)
Example @policies: {
posts: {
delete: :delete,
remove: :delete
},
application: {
admin: :check
}
}
19 20 21 |
# File 'lib/miau/storage.rb', line 19 def policies @policies end |
Instance Method Details
#add(klass, action, meth) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/miau/storage.rb', line 31 def add(klass, action, meth) kls = klass.name.underscore[0..-8] # remove "_policy" kls = kls.to_sym @policies[kls] ||= {} @instances[kls] ||= klass.new @policies[kls][action.to_sym] = meth.to_sym end |
#find_policy(klass, action) ⇒ Object
returns policy: [instance, method]
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/miau/storage.rb', line 49 def find_policy(klass, action) kls = instance_of(klass) act = policy_method(klass, action) return [kls, act] if kls.respond_to?(act) klass = :application kls = instance_of(klass) act = policy_method(klass, action) [kls, act] if kls.respond_to?(act) end |
#reset ⇒ Object
26 27 28 29 |
# File 'lib/miau/storage.rb', line 26 def reset @policies = {} @instances = {} end |
#run(klass, action, user, resource) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/miau/storage.rb', line 60 def run(klass, action, user, resource) arr = find_policy(klass, action) unless arr msg = "class <#{klass}> action <#{action}>" raise Miau::NotDefinedError, msg end policy, meth = arr policy.user = user policy.resource = resource policy.send(meth) end |
#to_yaml ⇒ Object
73 74 75 76 |
# File 'lib/miau/storage.rb', line 73 def to_yaml "# === @policies ===\n" + YAML.dump(@policies) + "# === @instances ===\n" + YAML.dump(@instances) end |