Class: HyperMesh::InternalClassPolicy

Inherits:
Object
  • Object
show all
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

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, &regulation|
    process_args(policy, [], args, regulation) do |model|
      get_ar_model(model).class_eval { define_method("#{method}_permitted?", &regulation) }
    end
  end
end

Instance Method Details

#allow_change(*args, &regulation) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/synchromesh/policy.rb', line 50

def allow_change(*args, &regulation)
  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?", &regulation) }
    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, &regulation) ⇒ Object



67
68
69
70
71
# File 'lib/synchromesh/policy.rb', line 67

def regulate(regulation_klass, policy, args, &regulation)
  process_args(policy, regulation_klass.allowed_opts, args, regulation) do |regulated_klass, opts|
    regulation_klass.add_regulation regulated_klass, opts, &regulation
  end
end

#regulate_all_broadcasts(*args, &regulation) ⇒ Object



27
28
29
# File 'lib/synchromesh/policy.rb', line 27

def regulate_all_broadcasts(*args, &regulation)
  regulate(ChannelBroadcastRegulation, :all_broadcasts, args, &regulation)
end

#regulate_broadcast(*args, &regulation) ⇒ Object



31
32
33
# File 'lib/synchromesh/policy.rb', line 31

def regulate_broadcast(*args, &regulation)
  regulate(InstanceBroadcastRegulation, :broadcast, args, &regulation)
end

#regulate_class_connection(*args, &regulation) ⇒ Object



15
16
17
# File 'lib/synchromesh/policy.rb', line 15

def regulate_class_connection(*args, &regulation)
  regulate(ClassConnectionRegulation, :class_connection, args, &regulation)
end

#regulate_instance_connections(*args, &regulation) ⇒ Object



23
24
25
# File 'lib/synchromesh/policy.rb', line 23

def regulate_instance_connections(*args, &regulation)
  regulate(InstanceConnectionRegulation, :instance_connections, args, &regulation)
end