Module: Conjur::DSL2::Types::AttributeDefinition

Included in:
Base
Defined in:
lib/conjur/dsl2/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.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/conjur/dsl2/types/base.rb', line 225

def attribute attr, options = {}
  type = options[:type]
  begin
    type ||= Conjur::DSL2::Types.const_get(attr.to_s.capitalize) 
  rescue NameError
  end
  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.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/conjur/dsl2/types/base.rb', line 170

def define_field attr, kind, type = nil, dsl_accessor = false
  register_yaml_field attr.to_s, type if type
  
  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(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(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.



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/conjur/dsl2/types/base.rb', line 201

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

#yaml_field?(name) ⇒ Boolean

Is there a Ruby type for a named field?

Returns:

  • (Boolean)


249
250
251
# File 'lib/conjur/dsl2/types/base.rb', line 249

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

#yaml_field_type(name) ⇒ Object

Ruby type for attribute name.



244
245
246
# File 'lib/conjur/dsl2/types/base.rb', line 244

def yaml_field_type name
  self.yaml_fields[name]
end