Module: Conjur::Policy::Types::AttributeDefinition

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

Overview

Define type-checked attributes, using the facilities defined in TypeChecking.

Instance Method Summary collapse

Instance Method Details

#attribute(attr, options = {}) ⇒ Object

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.



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# 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_field(attr, kind, type = nil, dsl_accessor = false) ⇒ Object

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.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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_plural_field(attr, kind, type = nil, dsl_accessor = false) ⇒ Object

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.



208
209
210
211
212
213
214
215
216
217
218
219
# 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

#field?(name) ⇒ Boolean

Is there a semantic kind for a named field?

Returns:

  • (Boolean)


262
263
264
# File 'lib/conjur/policy/types/base.rb', line 262

def field? name
  !!self.fields[name]
end

#yaml_field?(name) ⇒ Boolean

Is there a Ruby type for a named field?

Returns:

  • (Boolean)


257
258
259
# File 'lib/conjur/policy/types/base.rb', line 257

def yaml_field? name
  !!self.yaml_fields[name]
end

#yaml_field_type(name) ⇒ Object

Ruby type for attribute name.



252
253
254
# File 'lib/conjur/policy/types/base.rb', line 252

def yaml_field_type name
  self.yaml_fields[name]
end