Module: Conjur::Policy::Types::TypeChecking

Included in:
Base
Defined in:
lib/conjur/policy/types/base.rb

Overview

Methods which type-check and transform attributes. Type-checking can be done by duck-typing, with is_a?, or by a procedure.

Instance Method Summary collapse

Instance Method Details

#expect_array(kind, values) ⇒ Object

values can be an instance of type (as determined by the type-checking methods), or it must be an array of them.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/conjur/policy/types/base.rb', line 147

def expect_array kind, values
  # Hash gets converted to an array of key/value pairs by Array
  is_hash = values.kind_of?(Hash)
  values = [values] if is_hash

  result = Array(values).map do |v|
    send "expect_#{kind}", v
  end

  (values.is_a?(Array) and not is_hash) ? result : result[0]
end

#expect_boolean(v) ⇒ Object

v must be true or false.



137
138
139
140
141
142
143
# File 'lib/conjur/policy/types/base.rb', line 137

def expect_boolean v
  v = true if v == "true"
  v = false if v == "false"
  expect_type v, 
    "boolean",
    lambda{ [ true, false ].member?(v) }
end

#expect_hash(value) ⇒ Object

value can be a Hash, or an object which implements to_h.



129
130
131
132
133
134
# File 'lib/conjur/policy/types/base.rb', line 129

def expect_hash value
  expect_type value, 
    "hash",
    lambda{ value.is_a?(Hash)},
    lambda{ value.to_h.stringify_keys if value.respond_to?(:to_h) }
end

#expect_integer(value) ⇒ Object

value must be a Integer.



122
123
124
125
126
# File 'lib/conjur/policy/types/base.rb', line 122

def expect_integer value
  expect_type value, 
    "integer",
    Integer
end

#expect_layer(value) ⇒ Object

If it’s a Layer



85
86
87
# File 'lib/conjur/policy/types/base.rb', line 85

def expect_layer value
  expect_type value, "Layer", lambda{ value.is_a?(Layer) }
end

#expect_member(value) ⇒ Object

value may be a Member; Roles can also be converted to Members.



100
101
102
103
104
105
# File 'lib/conjur/policy/types/base.rb', line 100

def expect_member value
  expect_type value, 
    "Member", 
    Member,
    lambda{ Member.new(value) if test_role(value) }
end

#expect_permission(value) ⇒ Object

value must be a Permission.



108
109
110
111
112
# File 'lib/conjur/policy/types/base.rb', line 108

def expect_permission value
  expect_type value, 
    "Permission", 
    Permission
end

#expect_record(value) ⇒ Object

If it’s a Record



80
81
82
# File 'lib/conjur/policy/types/base.rb', line 80

def expect_record value
  expect_type value, "Record", lambda{ value.is_a?(Record) }
end

#expect_resource(value) ⇒ Object

If it looks like a resource.



90
91
92
# File 'lib/conjur/policy/types/base.rb', line 90

def expect_resource value
  expect_type value, "Resource", lambda{ test_resource value }
end

#expect_role(value) ⇒ Object

If it looks like a role.



95
96
97
# File 'lib/conjur/policy/types/base.rb', line 95

def expect_role value
  expect_type value, "Role", lambda{ test_role value }
end

#expect_string(value) ⇒ Object

value must be a String.



115
116
117
118
119
# File 'lib/conjur/policy/types/base.rb', line 115

def expect_string value
  expect_type value, 
    "string",
    String
end

#expect_type(value, type_name, test_function, converter = nil) ⇒ Object

This is the primary function of the module.

value an input value type_name used only for error messages. test_function a class or function which will determine if the value is already the correct type. converter if the test_function fails, the converter is called to coerce the type. It should return nil if its unable to do so.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/conjur/policy/types/base.rb', line 54

def expect_type value, type_name, test_function, converter = nil
  if test_function.is_a?(Class)
    cls = test_function
    test_function = lambda{ value.is_a?(cls) } 
  end
  if test_function.call
    value
  elsif converter && ( v = converter.call )
    v
  else
    name = value.class.respond_to?(:short_name) ? value.class.short_name : value.class.name
    raise "Expecting #{type_name}, got #{name}"
  end
end

#test_resource(r) ⇒ Object

Duck-type resources.



75
76
77
# File 'lib/conjur/policy/types/base.rb', line 75

def test_resource r
  r.respond_to?(:resource?) && r.resource?
end

#test_role(r) ⇒ Object

Duck-type roles.



70
71
72
# File 'lib/conjur/policy/types/base.rb', line 70

def test_role r
  r.respond_to?(:role?) && r.role?          
end