Class: Confluence::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/confluence/record.rb

Overview

Base class for working with Confluence records.

Direct Known Subclasses

Attachment, BlogEntry, Page, Space, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Record

Initializes a new record.

Parameters

hash<Hash>

A hash containing the attributes and its values. Keys can be Strings or Symbols.



60
61
62
63
64
65
66
67
# File 'lib/confluence/record.rb', line 60

def initialize(hash = {})
  @attributes = {}

  # iterate through each key/value pair and set attribute keyed by symbol
  hash.each_pair do |key, value|
    self[key.to_sym] = value
  end
end

Class Method Details

.clientObject

The client used for Confluence API calls.



8
9
10
11
# File 'lib/confluence/record.rb', line 8

def client
  raise "Confluence client is unavailable. Did you forget to use Confluence::Session.new?" unless @@client
  @@client
end

.client=(value) ⇒ Object

Sets the client.



15
16
17
# File 'lib/confluence/record.rb', line 15

def client=(value)
  @@client = value
end

.record_attr_accessor(*args) ⇒ Object

Defines an attr_accessor for a Record attribute.



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
# File 'lib/confluence/record.rb', line 21

def record_attr_accessor(*args)
  attributes = {}

  # iterate through each argument
  args.each do |arg|
    attributes = case arg
                 when Symbol
                   { arg => arg }
                 when Hash
                   arg
                 else
                   break
                 end

    attributes.each_pair do |key, name|
      class_eval %Q{
        def #{name}
          self[:#{key}]
        end

        def #{name}=(value)
          self[:#{key}] = value
        end
        }
    end
  end
end

Instance Method Details

#[](attr) ⇒ Object



69
70
71
# File 'lib/confluence/record.rb', line 69

def [](attr)
  @attributes[attr]
end

#[]=(attr, value) ⇒ Object



73
74
75
# File 'lib/confluence/record.rb', line 73

def []=(attr, value)
  @attributes[attr] = value
end

#clientObject

Returns the the Confluence API client.



52
53
54
# File 'lib/confluence/record.rb', line 52

def client
  Record.client
end

#labelsObject

Retrieves the labels of the record.



85
86
87
# File 'lib/confluence/record.rb', line 85

def labels
  @labels ||= client.getLabelsById(record_id).collect {|label| label["name"]}
end

#labels=(value) ⇒ Object

Sets the labels of the record.



91
92
93
94
95
96
97
98
99
# File 'lib/confluence/record.rb', line 91

def labels=(value)
  removed_labels = labels - value
  added_labels = value - labels

  client.removeLabelByName(removed_labels.join(" "), record_id) unless removed_labels.empty?
  client.addLabelByName(added_labels.join(" "), record_id) unless added_labels.empty?

  @labels = value
end

#record_idObject

Returns the id of the record.



79
80
81
# File 'lib/confluence/record.rb', line 79

def record_id
  self[:id]
end

#to_hashObject



101
102
103
104
105
# File 'lib/confluence/record.rb', line 101

def to_hash
  hash = Hash.new
  @attributes.each { |key, value| hash[key.to_s] = value }
  hash
end