Class: Jirarest2Field::CascadingField

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

Overview

TODO:

error message is more than just a bit misleading

The class to represent CascadingSelectFields

Instance Attribute Summary collapse

Attributes inherited from Field

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

Instance Method Summary collapse

Methods inherited from Field

#initialize, #value

Constructor Details

This class inherits a constructor from Jirarest2Field::Field

Instance Attribute Details

#allowed_values=(value) ⇒ Object (writeonly)



428
429
430
# File 'lib/jirarest2/field.rb', line 428

def allowed_values=(value)
  @allowed_values = value
end

#keyString (readonly)

The key element for the answers - It should not be needed - but it’s easer on the checks if it’s exposed

Returns:

  • (String)

    The key element for the way to Jira



425
426
427
# File 'lib/jirarest2/field.rb', line 425

def key
  @key
end

#value=(value) ⇒ Object (writeonly)

Set the value of the field



453
454
455
456
457
458
# File 'lib/jirarest2/field.rb', line 453

def value=(content)
  if ! content.instance_of?(Array) or content.size != 2 then
    raise Jirarest2::ValueNotAllowedException.new(@name,"Array"), "needs to be an Array with exactly 2 parameters. Was #{content.class}." 
  end
  super(content)
end

Instance Method Details

#createmeta(structure) ⇒ Object

TODO:

The implementation is awkward with only one element in the array and the fields as Hashes in element 0. See if this is should be rewritten.

Note:

fills allowed_values with a straight list of allowed values.

Interpret the result of createmeta for one field



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/jirarest2/field.rb', line 487

def createmeta(structure)
  @readonly = true if structure["operations"] == []
  @key = "value"
  if structure["allowedValues"] then
    @allowed_values << Hash.new
    structure["allowedValues"].each{ |suggestion|
      subentries = Array.new
      if suggestion.has_key?("children") then
        suggestion["children"].each{ |entry|
          subentries << entry["value"]
        }
      else
        subentries << nil
      end
      @allowed_values[0][suggestion[@key]] = subentries
    }
  end
end

#parse_value(jvalue) ⇒ Object

Parse the value of this field as sent by the server



478
479
480
481
# File 'lib/jirarest2/field.rb', line 478

def parse_value(jvalue)
  super
  @value = [jvalue["value"],jvalue["child"]["value"]]
end

#to_jHash, Nil

Representation to be used for json and jira

Returns:

  • (Hash)

    if the value is set

  • (Nil)

    if the value is not set



464
465
466
467
468
469
470
471
472
473
474
# File 'lib/jirarest2/field.rb', line 464

def to_j
  if @value.nil? then
    super(nil)
  else
    if @value[1].nil? then
      super({"value" => @value[0]}) # If there is no child the server prefers not to be bothered
    else
      super({"value" => @value[0], "child" => {"value" => @value[1]}})
    end
  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:



436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/jirarest2/field.rb', line 436

def value_allowed?(value)
  return true if @allowed_values == []  # If there is no list get out of here fast
  # Special case to ensure that we can use the string "nil" if the is really an allowed value
  if value[1] == "nil" && ( @allowed_values[0].has_key?(value[0]) && @allowed_values[0][value[0]] == [nil] ) then
    value[1] = nil
  end
  if @allowed_values[0].has_key?(value[0]) && @allowed_values[0][value[0]].include?(value[1]) then
    return true
  else
    raise Jirarest2::ValueNotAllowedException.new(@name,@allowed_values), "#{value.to_s} is not a valid value. Please use one of #{@allowed_values}"
  end
end