Class: Jirarest2Field::MultiField

Inherits:
Field
  • Object
show all
Defined in:
lib/jirarest2/field.rb

Overview

A field containing one or more other fields (usually only TextField or HashField)

Direct Known Subclasses

MultiHashField, MultiStringField

Instance Attribute Summary collapse

Attributes inherited from Field

#allowed_values, #id, #name, #raw_value, #readonly, #required

Instance Method Summary collapse

Methods inherited from Field

#createmeta, #parse_value, #value

Constructor Details

#initialize(id, name, args) ⇒ MultiField

Returns a new instance of MultiField.



318
319
320
321
322
# File 'lib/jirarest2/field.rb', line 318

def initialize(id,name,args)
  super(id,name,args)
  @value = []
  @delete = false
end

Instance Attribute Details

#value=(value) ⇒ Object (writeonly)

Set the value of the field



347
348
349
350
351
352
# File 'lib/jirarest2/field.rb', line 347

def value=(content)
  if ! content.instance_of?(Array) then
    content = [content]
  end
  super(content)
end

Instance Method Details

#<<(content) ⇒ Object

Add another field to the MultiField

Parameters:

  • content (Field, String)

    the content to add to the hash



395
396
397
398
399
400
401
402
# File 'lib/jirarest2/field.rb', line 395

def <<(content)
  
  if @value.length > 0  then
    raise Jirarest2::ValueNotAllowedException.new(@name,@value[0].class), "#{@value[0].class} vs #{content.class}"  if @value[0].class != content.class 
  end
  
  @value << content if value_allowed?(content)
end

#[](index) ⇒ Object

One field from the MultiField

Parameters:

  • index (Integer)

    Position of the field



406
407
408
# File 'lib/jirarest2/field.rb', line 406

def [](index)
  @value[index]
end

#[]=(index, content) ⇒ Object

Set the content of one special field

Parameters:

  • index (Integer)

    Position of the field

  • content (Object)

    Value to put at the place marked by index

Raises:



414
415
416
417
# File 'lib/jirarest2/field.rb', line 414

def []=(index,content)
  raise Jirarest2::ValueNotAllowedException.new(@name,@value[0].class), "#{@value[0].class} vs #{content.class}" if @value[0].class != content.class 
  @value[index] = content if value_allowed?(content)
end

#delete(object) ⇒ Object

Delete items

Parameters:

  • object (Object)

    The object to delete (If the object is self it all fields and sets @delete)

Returns:

  • The deleted object



377
378
379
380
381
382
383
384
# File 'lib/jirarest2/field.rb', line 377

def delete(object)
  if object == self then
    @delete = true
    @value = []
  else
    @value.delete(object)
  end
end

#delete_by_value(ovalue) ⇒ Array

Delete items based on their value attribute

Parameters:

  • ovalue (Object)

    The value that defines the objects to delete from this MultiField

Returns:

  • (Array)

    The remaining Objects



389
390
391
# File 'lib/jirarest2/field.rb', line 389

def delete_by_value(ovalue)
  @value.delete_if {|x| x.value == ovalue}
end

#to_j(value = @value) ⇒ Object

Return for JSON representation if @value == [] or nil and @delete is false set super will return nil



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/jirarest2/field.rb', line 357

def to_j(value = @value)
  if ((value == [] || value.nil?) and ! @delete) then
    super(nil)
  else
    value.compact!
    fields = Array.new
    if value[0].class  < Jirarest2Field::Field then
      value.each {|field| 
        fields << field.to_j_inner
      }
    else
      fields = value
    end
    super(fields)
  end
end

#value_allowed?(value) ⇒ Boolean

Checks if the value is in the list of allowed values. If the list is empty every value is allowed

Parameters:

  • value (Object)

    The value to check for

Returns:

  • (Boolean)

    true if the value is allowed, false if not

Raises:



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/jirarest2/field.rb', line 328

def value_allowed?(value)
  return true if @allowed_values == [] # If there is no list get out of here fast
  if value.instance_of?(Array) then
    value.each { |entry|
      value_allowed?(entry)
    }
  else
    if @allowed_values.include?(value) then
      return true
    else
      raise Jirarest2::ValueNotAllowedException.new(@name,@allowed_values), "#{value} is not a valid value. Please use one of #{@allowed_values.join("; ").to_s}"
    end
  end
end