Class: Jirarest2Field::Field

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

Overview

Superclass for all fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, args) ⇒ Field

Returns a new instance of Field.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jirarest2/field.rb', line 45

def initialize(id,name,args)
  @id = id
  @name = name
  if args[:required] then
    @required = true
  else
    @required = false
  end
  @allowed_values = []
  if args[:allowed_values] then
    allowed_values = args[:allowed_values]
  end
  @value = nil
  @readonly = false
  if args[:createmeta] then
    createmeta(args[:createmeta])
  end
end

Instance Attribute Details

#allowed_valuesArray

Allowed values for the fields

Returns:

  • (Array)

    The values allowed for this kind of field



40
41
42
# File 'lib/jirarest2/field.rb', line 40

def allowed_values
  @allowed_values
end

#idString (readonly)

The field id in JIRA(tm)

Returns:

  • (String)

    The id in your JIRA(tm) instance



31
32
33
# File 'lib/jirarest2/field.rb', line 31

def id
  @id
end

#nameString (readonly)

The name given to the field (not unique in jira!)

Returns:

  • (String)

    The name in your JIRA(tm) instance



34
35
36
# File 'lib/jirarest2/field.rb', line 34

def name
  @name
end

#raw_valueHash (readonly)

The raw value

Returns:

  • (Hash)

    The value in it’s raw form



37
38
39
# File 'lib/jirarest2/field.rb', line 37

def raw_value
  @raw_value
end

#readonlyBoolean (readonly)

Is this field readonly?

Returns:

  • (Boolean)

    Default: false



28
29
30
# File 'lib/jirarest2/field.rb', line 28

def readonly
  @readonly
end

#requiredBoolean (readonly)

Is this field mandatory?

Returns:

  • (Boolean)

    Default: false. True if your field has to be set for the issuetype



25
26
27
# File 'lib/jirarest2/field.rb', line 25

def required
  @required
end

Instance Method Details

#createmeta(structure) ⇒ Object

TODO:

change @allowed_values here. -> suggestion has to go to and build the correct type

Interpret the result of createmeta for one field If there is only one value allowed this value will be set



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jirarest2/field.rb', line 107

def createmeta(structure)
  @readonly = true if structure["operations"] == []
  if structure["allowedValues"] then
    structure["allowedValues"].flatten!(1)
    if ! structure["allowedValues"][0].nil? then
      if structure["allowedValues"][0].has_key?("value") then 
        @key = "value"
      elsif structure["allowedValues"][0].has_key?("key") then
        @key = "key"
      elsif structure["allowedValues"][0].has_key?("name") then 
        @key = "name"
      else
        @key = "id"
      end
      structure["allowedValues"].each{ |suggestion|
        @allowed_values << suggestion[@key]
      }
      if structure["allowedValues"].size == 1 && !structure["allowedValues"][0].instance_of?(Array) then # If there is only one value allowed it might as well be set at the earliest convenience
        @value = structure["allowedValues"][0][@key] 
      end
    else
      @key = ""
      @allowed_values == []
    end
  end
end

#parse_value(jvalue) ⇒ Object

Parse the value of this field as sent by the server



136
137
138
139
# File 'lib/jirarest2/field.rb', line 136

def parse_value(jvalue)
  @rawvalue = jvalue
  @value = jvalue
end

#to_j(value = @value) ⇒ Hash, Nil

Representation to be used for json and jira

Parameters:

  • value (String, Hash) (defaults to: @value)

    the to be put into the representation.

Returns:

  • (Hash)

    if the value is set

  • (Nil)

    if the value is not set



95
96
97
98
99
100
101
# File 'lib/jirarest2/field.rb', line 95

def to_j(value = @value)
  if value.nil? then
    return nil
  else 
    return {@id => value}
  end
end

#value(raw = false) ⇒ String, Object

Get the value of the field

Parameters:

  • raw (Boolean) (defaults to: false)

    true returns a date Object, false a String

Returns:

  • (String)

    if raw is false (default)

  • (Object)

    if raw is true



68
69
70
# File 'lib/jirarest2/field.rb', line 68

def value(raw = false)
  return @value
end

#value=(content) ⇒ Object

Set the value of the field

Parameters:

  • content (Object)

    The value of this field



87
88
89
# File 'lib/jirarest2/field.rb', line 87

def value=(content)
  @value = content if value_allowed?(content)
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:



76
77
78
79
80
81
82
83
# File 'lib/jirarest2/field.rb', line 76

def value_allowed?(value)
  return true if @allowed_values  == [] # If there is no list get out of here fast
  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