module Conjur::DSL2::Types::TypeChecking
Methods which type-check and transform attributes. Type-checking can be done by duck-typing, with is_a?
, or by a procedure.
Public Instance Methods
values
can be an instance of type
(as determined by the type-checking methods), or it must be an array of them.
# File lib/conjur/dsl2/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
v
must be true
or false
.
# File lib/conjur/dsl2/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
value
can be a Hash, or an object which implements to_h
.
# File lib/conjur/dsl2/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
value
must be a Integer.
# File lib/conjur/dsl2/types/base.rb, line 122 def expect_integer value expect_type value, "integer", Integer end
If it’s a Layer
# File lib/conjur/dsl2/types/base.rb, line 85 def expect_layer value expect_type value, "Layer", lambda{ value.is_a?(Layer) } end
value
may be a Member
; Roles can also be converted to Members.
# File lib/conjur/dsl2/types/base.rb, line 100 def expect_member value expect_type value, "Member", Member, lambda{ Member.new(value) if test_role(value) } end
value
must be a Permission.
# File lib/conjur/dsl2/types/base.rb, line 108 def expect_permission value expect_type value, "Permission", Permission end
If it’s a Record
# File lib/conjur/dsl2/types/base.rb, line 80 def expect_record value expect_type value, "Record", lambda{ value.is_a?(Record) } end
If it looks like a resource.
# File lib/conjur/dsl2/types/base.rb, line 90 def expect_resource value expect_type value, "Resource", lambda{ test_resource value } end
If it looks like a role.
# File lib/conjur/dsl2/types/base.rb, line 95 def expect_role value expect_type value, "Role", lambda{ test_role value } end
value
must be a String.
# File lib/conjur/dsl2/types/base.rb, line 115 def expect_string value expect_type value, "string", String end
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.
# File lib/conjur/dsl2/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
Duck-type resources.
# File lib/conjur/dsl2/types/base.rb, line 75 def test_resource r r.respond_to?(:resource?) && r.resource? end
Duck-type roles.
# File lib/conjur/dsl2/types/base.rb, line 70 def test_role r r.respond_to?(:role?) && r.role? end