Class: Hyperloop::InternalClassPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper-operation/transport/policy.rb

Constant Summary collapse

EXPOSED_METHODS =
[
  :regulate_class_connection, :always_allow_connection, :regulate_instance_connections,
  :regulate_all_broadcasts, :regulate_broadcast,
  :dispatch_to, :regulate_dispatches_from, :always_dispatch_from,
  :allow_change, :allow_create, :allow_read, :allow_update, :allow_destroy
]
CHANGE_POLICIES =
[:create, :update, :destroy]

Instance Attribute Summary collapse

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/hyper-operation/transport/policy.rb', line 5

def initialize(regulated_klass)
  @regulated_klass = regulated_klass
end

Instance Attribute Details

#regulated_klassObject (readonly)

Returns the value of attribute regulated_klass.



9
10
11
# File 'lib/hyper-operation/transport/policy.rb', line 9

def regulated_klass
  @regulated_klass
end

Class Method Details

.allow_policy(policy, method) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/hyper-operation/transport/policy.rb', line 62

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



74
75
76
77
78
79
80
81
82
83
# File 'lib/hyper-operation/transport/policy.rb', line 74

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



22
23
24
# File 'lib/hyper-operation/transport/policy.rb', line 22

def always_allow_connection(*args)
  regulate(ClassConnectionRegulation, nil, args) { true }
end

#always_dispatch_from(*args, &regulation) ⇒ Object



47
48
49
# File 'lib/hyper-operation/transport/policy.rb', line 47

def always_dispatch_from(*args, &regulation)
  regulate_dispatches_from(*args) { true }
end

#check_valid_on_option(policy) ⇒ Object



124
125
126
127
128
129
# File 'lib/hyper-operation/transport/policy.rb', line 124

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

#dispatch_to(*args, &regulation) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/hyper-operation/transport/policy.rb', line 51

def dispatch_to(*args, &regulation)
  actual_klass = regulated_klass.is_a?(Class) ? regulated_klass : regulated_klass.constantize rescue nil
  actual_klass.dispatch_to(actual_klass) if actual_klass.respond_to? :dispatch_to
  unless actual_klass.respond_to? :dispatch_to
    raise 'you can only dispatch_to Operation classes'
  end
  actual_klass.dispatch_to(*args, &regulation)
end

#get_ar_model(str) ⇒ Object



85
86
87
88
89
# File 'lib/hyper-operation/transport/policy.rb', line 85

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



97
98
99
100
101
102
103
104
105
# File 'lib/hyper-operation/transport/policy.rb', line 97

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hyper-operation/transport/policy.rb', line 107

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



91
92
93
94
95
# File 'lib/hyper-operation/transport/policy.rb', line 91

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



30
31
32
# File 'lib/hyper-operation/transport/policy.rb', line 30

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

#regulate_broadcast(*args, &regulation) ⇒ Object



34
35
36
# File 'lib/hyper-operation/transport/policy.rb', line 34

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

#regulate_class_connection(*args, &regulation) ⇒ Object



18
19
20
# File 'lib/hyper-operation/transport/policy.rb', line 18

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

#regulate_dispatches_from(*args, &regulation) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/hyper-operation/transport/policy.rb', line 38

def regulate_dispatches_from(*args, &regulation)
  args.each do |klass|
    unless klass.respond_to? :dispatch_to
      raise 'you can only regulate_dispatches_from Operation classes'
    end
    klass._dispatch_to(self) { |sself| sself.regulated_klass if instance_eval(&regulation) }
  end
end

#regulate_instance_connections(*args, &regulation) ⇒ Object



26
27
28
# File 'lib/hyper-operation/transport/policy.rb', line 26

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