Class: HyperMesh::InternalClassPolicy
- Defined in:
- lib/synchromesh/policy.rb
Constant Summary collapse
- EXPOSED_METHODS =
[ :regulate_class_connection, :always_allow_connection, :regulate_instance_connections, :regulate_all_broadcasts, :regulate_broadcast, :allow_change, :allow_create, :allow_read, :allow_update, :allow_destroy ]
- CHANGE_POLICIES =
[:create, :update, :destroy]
Class Method Summary collapse
Instance Method Summary collapse
- #allow_change(*args, ®ulation) ⇒ Object
- #always_allow_connection(*args) ⇒ Object
- #check_valid_on_option(policy) ⇒ Object
- #get_ar_model(str) ⇒ Object
-
#initialize(regulated_klass) ⇒ InternalClassPolicy
constructor
A new instance of InternalClassPolicy.
- #process_args(policy, allowed_opts, args, regulation) ⇒ Object
- #process_to_opt(allowed_opts, opts, args) ⇒ Object
- #regulate(regulation_klass, policy, args, ®ulation) ⇒ Object
- #regulate_all_broadcasts(*args, ®ulation) ⇒ Object
- #regulate_broadcast(*args, ®ulation) ⇒ Object
- #regulate_class_connection(*args, ®ulation) ⇒ Object
- #regulate_instance_connections(*args, ®ulation) ⇒ Object
Constructor Details
#initialize(regulated_klass) ⇒ InternalClassPolicy
Returns a new instance of InternalClassPolicy.
5 6 7 |
# File 'lib/synchromesh/policy.rb', line 5 def initialize(regulated_klass) @regulated_klass = regulated_klass end |
Class Method Details
.allow_policy(policy, method) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/synchromesh/policy.rb', line 38 def self.allow_policy(policy, method) define_method "allow_#{policy}" do |*args, ®ulation| process_args(policy, [], args, regulation) do |model| get_ar_model(model).class_eval { define_method("#{method}_permitted?", ®ulation) } end end end |
Instance Method Details
#allow_change(*args, ®ulation) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/synchromesh/policy.rb', line 50 def allow_change(*args, ®ulation) process_args('change', [:on], args, regulation) do |model, opts| model = get_ar_model(model) opts[:on] ||= CHANGE_POLICIES opts[:on].each do |policy| check_valid_on_option policy model.class_eval { define_method("#{policy}_permitted?", ®ulation) } end end end |
#always_allow_connection(*args) ⇒ Object
19 20 21 |
# File 'lib/synchromesh/policy.rb', line 19 def always_allow_connection(*args) regulate(ClassConnectionRegulation, nil, args) { true } end |
#check_valid_on_option(policy) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/synchromesh/policy.rb', line 100 def check_valid_on_option(policy) unless CHANGE_POLICIES.include? policy valid_policies = CHANGE_POLICIES.collect { |s| ":{s}" }.to_sentence raise "only #{valid_policies} are allowed on the regulate_access :on option" end end |
#get_ar_model(str) ⇒ Object
61 62 63 64 65 |
# File 'lib/synchromesh/policy.rb', line 61 def get_ar_model(str) str.is_a?(Class) ? str : Object.const_get(str) rescue raise "#{str} is not a class" end |
#process_args(policy, allowed_opts, args, regulation) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/synchromesh/policy.rb', line 73 def process_args(policy, allowed_opts, args, regulation) raise "you must provide a block to the regulate_#{policy} method" unless regulation *args, opts = args if args.last.is_a? Hash opts ||= {} args = process_to_opt(allowed_opts, opts, args) args.each do |regulated_klass| yield regulated_klass, opts end end |
#process_to_opt(allowed_opts, opts, args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/synchromesh/policy.rb', line 83 def process_to_opt(allowed_opts, opts, args) opts.each do |opt, value| unless opt == :to || allowed_opts.include?(opt) raise "Unknown ':#{opt} => #{value}' option in policy definition" end end if (to = opts[:to]) raise "option to: :#{to} is not recognized in allow_#{policy}" unless to == :all raise "option to: :all cannot be used with other classes" unless args.empty? [ActiveRecord::Base] elsif args.empty? [@regulated_klass] else args end end |
#regulate(regulation_klass, policy, args, ®ulation) ⇒ Object
67 68 69 70 71 |
# File 'lib/synchromesh/policy.rb', line 67 def regulate(regulation_klass, policy, args, ®ulation) process_args(policy, regulation_klass.allowed_opts, args, regulation) do |regulated_klass, opts| regulation_klass.add_regulation regulated_klass, opts, ®ulation end end |
#regulate_all_broadcasts(*args, ®ulation) ⇒ Object
27 28 29 |
# File 'lib/synchromesh/policy.rb', line 27 def regulate_all_broadcasts(*args, ®ulation) regulate(ChannelBroadcastRegulation, :all_broadcasts, args, ®ulation) end |
#regulate_broadcast(*args, ®ulation) ⇒ Object
31 32 33 |
# File 'lib/synchromesh/policy.rb', line 31 def regulate_broadcast(*args, ®ulation) regulate(InstanceBroadcastRegulation, :broadcast, args, ®ulation) end |
#regulate_class_connection(*args, ®ulation) ⇒ Object
15 16 17 |
# File 'lib/synchromesh/policy.rb', line 15 def regulate_class_connection(*args, ®ulation) regulate(ClassConnectionRegulation, :class_connection, args, ®ulation) end |
#regulate_instance_connections(*args, ®ulation) ⇒ Object
23 24 25 |
# File 'lib/synchromesh/policy.rb', line 23 def regulate_instance_connections(*args, ®ulation) regulate(InstanceConnectionRegulation, :instance_connections, args, ®ulation) end |