Class: Trello::Label

Inherits:
BasicData show all
Defined in:
lib/trello/label.rb

Overview

A colored Label attached to a card

Constant Summary collapse

SYMBOL_TO_STRING =
{
  id: 'id',
  name: 'name',
  board_id: 'idBoard',
  color: 'color',
  uses: 'uses'
}
VALID_LABEL_COLOURS =
%w{green yellow orange red purple blue sky lime pink black} << ''

Instance Attribute Summary collapse

Attributes inherited from BasicData

#client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicData

#==, client, #initialize, many, one, parse, parse_many, path_name, #refresh!, register_attributes, save

Methods included from JsonUtils

included

Constructor Details

This class inherits a constructor from Trello::BasicData

Instance Attribute Details

#colorString

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/trello/label.rb', line 9

class Label < BasicData
  register_attributes :id, :name, :board_id, :uses,
    readonly: [ :id, :uses, :board_id ]
  validates_presence_of :id, :uses, :board_id, :name
  validates_length_of   :name,        in: 1..16384

  SYMBOL_TO_STRING = {
    id: 'id',
    name: 'name',
    board_id: 'idBoard',
    color: 'color',
    uses: 'uses'
  }

  class << self
    VALID_LABEL_COLOURS = %w{green yellow orange red purple blue sky lime pink black} << ''

    # Find a specific card by its id.
    def find(id, params = {})
      client.find(:label, id, params)
    end

    # Create a new label and save it on Trello.
    def create(options)
      client.create(:label,
        'name' => options[:name],
        'idBoard' => options[:board_id],
        'color'   => options[:color],
      )
    end

    # Label colours
    def label_colours
      VALID_LABEL_COLOURS
    end
  end

  define_attribute_methods [:color]

  def color
    @attributes[:color]
  end

  def color= colour
    unless Label.label_colours.include? colour
      errors.add(:label, "color '#{colour}' does not exist")
      return Trello.logger.warn "The label colour '#{colour}' does not exist."
    end

    self.send(:"color_will_change!") unless colour == @attributes[:color]
    @attributes[:color] = colour
  end

  # Update the fields of a label.
  #
  # Supply a hash of stringkeyed data retrieved from the Trello API representing
  # a label.
  def update_fields(fields)
    attributes[:id] = fields['id']
    attributes[:name]  = fields['name'] || fields[:name]
    attributes[:color] = fields['color'] || fields[:color]
    attributes[:board_id] = fields['idBoard'] || fields[:board_id]
    attributes[:uses] = fields['uses']
    self
  end

  # Returns a reference to the board this label is currently connected.
  one :board, path: :boards, using: :board_id

  # Saves a record.
  def save
    # If we have an id, just update our fields.
    return update! if id

    from_response client.post("/labels", {
      name:   name,
      color:   color,
      idBoard: board_id,
    })
  end

  # Update an existing record.
  # Warning, this updates all fields using values already in memory. If
  # an external resource has updated these fields, you should refresh!
  # this object before making your changes, and before updating the record.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear

    client.put("/labels/#{id}", payload)
  end

  # Delete this label
  def delete
    client.delete("/labels/#{id}")
  end
end

#idString

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/trello/label.rb', line 9

class Label < BasicData
  register_attributes :id, :name, :board_id, :uses,
    readonly: [ :id, :uses, :board_id ]
  validates_presence_of :id, :uses, :board_id, :name
  validates_length_of   :name,        in: 1..16384

  SYMBOL_TO_STRING = {
    id: 'id',
    name: 'name',
    board_id: 'idBoard',
    color: 'color',
    uses: 'uses'
  }

  class << self
    VALID_LABEL_COLOURS = %w{green yellow orange red purple blue sky lime pink black} << ''

    # Find a specific card by its id.
    def find(id, params = {})
      client.find(:label, id, params)
    end

    # Create a new label and save it on Trello.
    def create(options)
      client.create(:label,
        'name' => options[:name],
        'idBoard' => options[:board_id],
        'color'   => options[:color],
      )
    end

    # Label colours
    def label_colours
      VALID_LABEL_COLOURS
    end
  end

  define_attribute_methods [:color]

  def color
    @attributes[:color]
  end

  def color= colour
    unless Label.label_colours.include? colour
      errors.add(:label, "color '#{colour}' does not exist")
      return Trello.logger.warn "The label colour '#{colour}' does not exist."
    end

    self.send(:"color_will_change!") unless colour == @attributes[:color]
    @attributes[:color] = colour
  end

  # Update the fields of a label.
  #
  # Supply a hash of stringkeyed data retrieved from the Trello API representing
  # a label.
  def update_fields(fields)
    attributes[:id] = fields['id']
    attributes[:name]  = fields['name'] || fields[:name]
    attributes[:color] = fields['color'] || fields[:color]
    attributes[:board_id] = fields['idBoard'] || fields[:board_id]
    attributes[:uses] = fields['uses']
    self
  end

  # Returns a reference to the board this label is currently connected.
  one :board, path: :boards, using: :board_id

  # Saves a record.
  def save
    # If we have an id, just update our fields.
    return update! if id

    from_response client.post("/labels", {
      name:   name,
      color:   color,
      idBoard: board_id,
    })
  end

  # Update an existing record.
  # Warning, this updates all fields using values already in memory. If
  # an external resource has updated these fields, you should refresh!
  # this object before making your changes, and before updating the record.
  def update!
    @previously_changed = changes
    # extract only new values to build payload
    payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
    @changed_attributes.clear

    client.put("/labels/#{id}", payload)
  end

  # Delete this label
  def delete
    client.delete("/labels/#{id}")
  end
end

Class Method Details

.create(options) ⇒ Object

Create a new label and save it on Trello.



32
33
34
35
36
37
38
# File 'lib/trello/label.rb', line 32

def create(options)
  client.create(:label,
    'name' => options[:name],
    'idBoard' => options[:board_id],
    'color'   => options[:color],
  )
end

.find(id, params = {}) ⇒ Object

Find a specific card by its id.



27
28
29
# File 'lib/trello/label.rb', line 27

def find(id, params = {})
  client.find(:label, id, params)
end

.label_coloursObject

Label colours



41
42
43
# File 'lib/trello/label.rb', line 41

def label_colours
  VALID_LABEL_COLOURS
end

Instance Method Details

#deleteObject

Delete this label



104
105
106
# File 'lib/trello/label.rb', line 104

def delete
  client.delete("/labels/#{id}")
end

#saveObject

Saves a record.



79
80
81
82
83
84
85
86
87
88
# File 'lib/trello/label.rb', line 79

def save
  # If we have an id, just update our fields.
  return update! if id

  from_response client.post("/labels", {
    name:   name,
    color:   color,
    idBoard: board_id,
  })
end

#update!Object

Update an existing record. Warning, this updates all fields using values already in memory. If an external resource has updated these fields, you should refresh! this object before making your changes, and before updating the record.



94
95
96
97
98
99
100
101
# File 'lib/trello/label.rb', line 94

def update!
  @previously_changed = changes
  # extract only new values to build payload
  payload = Hash[changes.map { |key, values| [SYMBOL_TO_STRING[key.to_sym].to_sym, values[1]] }]
  @changed_attributes.clear

  client.put("/labels/#{id}", payload)
end

#update_fields(fields) ⇒ Object

Update the fields of a label.

Supply a hash of stringkeyed data retrieved from the Trello API representing a label.



66
67
68
69
70
71
72
73
# File 'lib/trello/label.rb', line 66

def update_fields(fields)
  attributes[:id] = fields['id']
  attributes[:name]  = fields['name'] || fields[:name]
  attributes[:color] = fields['color'] || fields[:color]
  attributes[:board_id] = fields['idBoard'] || fields[:board_id]
  attributes[:uses] = fields['uses']
  self
end