module Conjur::Policy::Types::AttributeDefinition
Define type-checked attributes, using the facilities defined in TypeChecking
.
Public Instance Methods
This is the primary method used by concrete types to define their attributes.
attr
the singularized attribute name.
Options: type
a structured type to be constructed by the parser. If not provided, the type may be inferred from the attribute name (e.g. an attribute called :member is the type Member
). kind
the symbolic name of the type. Inferred from the type, if the type is provided. Otherwise it's mandatory. singular
by default, attributes accept multiple values. This flag restricts the attribute to a single value only.
# File lib/conjur/policy/types/base.rb, line 232 def attribute attr, options = {} type = options[:type] begin type ||= Conjur::Policy::Types.const_get(attr.to_s.capitalize) rescue NameError end type = nil if type == String kind = options[:kind] kind ||= type.short_name.downcase.to_sym if type raise "Attribute :kind must be defined, explicitly or inferred from :type" unless kind if options[:singular] define_field attr, kind, type, options[:dsl_accessor] else define_plural_field attr, kind, type, options[:dsl_accessor] end end
Define a singular field.
attr
the name of the field kind
the type of the field, which corresponds to a TypeChecking
method. type
a DSL object type which the parser should use to process the field. This option is not used for simple kinds like :boolean and :string, because they are not structured objects.
# File lib/conjur/policy/types/base.rb, line 176 def define_field attr, kind, type = nil, dsl_accessor = false register_yaml_field attr.to_s, type if type register_field attr.to_s, kind if kind if dsl_accessor define_method attr do |*args| v = args.shift if v existing = self.instance_variable_get("@#{attr}") value = if existing Array(existing) + [ v ] else v end self.instance_variable_set("@#{attr}", self.class.expect_array(attr, kind, value)) else self.instance_variable_get("@#{attr}") end end else define_method attr do self.instance_variable_get("@#{attr}") end end define_method "#{attr}=" do |v| self.instance_variable_set("@#{attr}", self.class.expect_array(attr, kind, v)) end end
Define a plural field. A plural field is basically just an alias to the singular field. For example, a plural field called members
is really just an alias to member
. Both member
and members
will accept single values or Arrays of values.
# File lib/conjur/policy/types/base.rb, line 208 def define_plural_field attr, kind, type = nil, dsl_accessor = false define_field attr, kind.to_s, type, dsl_accessor register_yaml_field attr.to_s.pluralize, type if type define_method attr.to_s.pluralize do |*args| send attr, *args end define_method "#{attr.to_s.pluralize}=" do |v| send "#{attr}=", v end end
Is there a semantic kind for a named field?
# File lib/conjur/policy/types/base.rb, line 262 def field? name !!self.fields[name] end
Is there a Ruby type for a named field?
# File lib/conjur/policy/types/base.rb, line 257 def yaml_field? name !!self.yaml_fields[name] end
Ruby type for attribute name.
# File lib/conjur/policy/types/base.rb, line 252 def yaml_field_type name self.yaml_fields[name] end
Protected Instance Methods
nodoc
# File lib/conjur/policy/types/base.rb, line 275 def register_field field_name, kind raise "YAML field #{field_name} already defined on #{self.name} as #{self.fields[field_name]}" if self.field?(field_name) self.fields[field_name] = kind end
nodoc
# File lib/conjur/policy/types/base.rb, line 269 def register_yaml_field field_name, type raise "YAML field #{field_name} already defined on #{self.name} as #{self.yaml_fields[field_name]}" if self.yaml_field?(field_name) self.yaml_fields[field_name] = type end