Class: Fencepost::Gate

Inherits:
Object
  • Object
show all
Defined in:
lib/fencepost/gate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_list, params) ⇒ Gate

Returns a new instance of Gate.


4
5
6
7
8
9
# File 'lib/fencepost/gate.rb', line 4

def initialize(model_list, params)
  @allow_array = []
  @deny_array = []
  @model_list = model_list
  @model = model
end

Instance Attribute Details

#allow_arrayObject (readonly)

Returns the value of attribute allow_array


3
4
5
# File 'lib/fencepost/gate.rb', line 3

def allow_array
  @allow_array
end

#deny_arrayObject (readonly)

Returns the value of attribute deny_array


3
4
5
# File 'lib/fencepost/gate.rb', line 3

def deny_array
  @deny_array
end

#modelObject (readonly)

Returns the value of attribute model


3
4
5
# File 'lib/fencepost/gate.rb', line 3

def model
  @model
end

#model_listObject (readonly)

Returns the value of attribute model_list


3
4
5
# File 'lib/fencepost/gate.rb', line 3

def model_list
  @model_list
end

Instance Method Details

#allow(*elements) ⇒ Object


17
18
19
20
# File 'lib/fencepost/gate.rb', line 17

def allow(*elements)
  @allow_array += elements
  self
end

#allow_permissions(perms) ⇒ Object


27
28
29
30
31
32
# File 'lib/fencepost/gate.rb', line 27

def allow_permissions(perms)
  hash_values(allow_array).each do |hash_value|
    perms = nested_operations(perms, hash_value, :+)
  end
  perms + scalar_values(allow_array)
end

#build_permits(model, permits_array) ⇒ Object


72
73
74
75
76
77
78
79
80
81
# File 'lib/fencepost/gate.rb', line 72

def build_permits(model, permits_array)
  model_attributes(model).each {|k| permits_array << k }
  model_nested_attributes(model).each do |nao, value|
    node = get_node(:nested_collection_name, nao)
    node = get_node(:demodulized_name, nao) if node.keys.size == 0
    node = node.values[0]
    permits_array << {node[:nested_attributes_name] => build_permits(node[:model], [])}
  end
  permits_array
end

#deny(*elements) ⇒ Object


22
23
24
25
# File 'lib/fencepost/gate.rb', line 22

def deny(*elements)
  @deny_array += elements
  self
end

#deny_permissions(perms) ⇒ Object


34
35
36
37
38
39
# File 'lib/fencepost/gate.rb', line 34

def deny_permissions(perms)
  hash_values(deny_array).each do |hash_value|
    perms = nested_operations(perms, hash_value, :-)
  end
  perms - scalar_values(deny_array)
end

#get_node(key, value) ⇒ Object


91
92
93
# File 'lib/fencepost/gate.rb', line 91

def get_node(key, value)
  self.model_list.select {|k,v| v[key] == value}
end

#hash_values(arry) ⇒ Object


45
46
47
# File 'lib/fencepost/gate.rb', line 45

def hash_values(arry)
  arry.select{|x| x.is_a?(Hash)}
end

#model_attributes(model) ⇒ Object


83
84
85
# File 'lib/fencepost/gate.rb', line 83

def model_attributes(model)
  get_node(:model, model).values[0][:attributes]
end

#model_nested_attributes(model) ⇒ Object


87
88
89
# File 'lib/fencepost/gate.rb', line 87

def model_nested_attributes(model)
  get_node(:model, model).values[0][:nested_attributes_options]
end

#nested_operations(perms, hash, operator) ⇒ Object


49
50
51
52
53
54
55
# File 'lib/fencepost/gate.rb', line 49

def nested_operations(perms, hash, operator)
  perms.each do |perm|
    hash.each do |key, value|
      set_permission_value(perm, key, value, operator)
    end
  end
end

#open(params, key, model) ⇒ Object


11
12
13
14
15
# File 'lib/fencepost/gate.rb', line 11

def open(params, key, model)
  perms = deny_permissions(allow_permissions(build_permits(model, [])))
  params = params.permit!.to_h if params.respond_to?(:permit!)
  ActionController::Parameters.new(params).require(key).permit(perms)
end

#recurse_permissions(perm, key, value, operator) ⇒ Object


64
65
66
67
68
69
70
# File 'lib/fencepost/gate.rb', line 64

def recurse_permissions(perm, key, value, operator)
  hash_values(perm[key]).each do |pk|
    hash_values(value).each do |hk|
      set_permission_value(pk, hk.keys[0], hk.values[0], operator)
    end
  end
end

#scalar_values(arry) ⇒ Object


41
42
43
# File 'lib/fencepost/gate.rb', line 41

def scalar_values(arry)
  arry.select{|x| x.is_a?(String) || x.is_a?(Symbol)}
end

#set_permission_value(perm, key, value, operator) ⇒ Object


57
58
59
60
61
62
# File 'lib/fencepost/gate.rb', line 57

def set_permission_value(perm, key, value, operator)
  if perm.is_a?(Hash) && perm.keys.index(key) && perm[key].is_a?(Array)
    perm[key] = perm[key].send(operator, value)
    recurse_permissions(perm, key, value, operator)
  end
end