Class: Confluence::RemoteDataObject

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

Overview

Abstract object representing some piece of data in Confluence. This must be overridden by a child class that defines values for the class attributes save_method and get_method and/or implements its own get and save methods.

Direct Known Subclasses

Page, User

Constant Summary collapse

@@connector =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_object = nil) ⇒ RemoteDataObject

Returns a new instance of RemoteDataObject.



49
50
51
52
# File 'lib/confluence/confluence_remote_data_object.rb', line 49

def initialize(data_object = nil)
  self.attributes = {}
  load_from_object(data_object) unless data_object.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/confluence/confluence_remote_data_object.rb', line 117

def method_missing(name, *args)
  if name.to_s =~ /^(.*?)=$/
    self[$1.intern] = args[0]
  elsif name.to_s =~ /^[\w_]+$/
    self[name]
  else
    raise NoMethodError, name.to_s
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



14
15
16
# File 'lib/confluence/confluence_remote_data_object.rb', line 14

def attributes
  @attributes
end

#confluenceObject

Returns the value of attribute confluence.



16
17
18
# File 'lib/confluence/confluence_remote_data_object.rb', line 16

def confluence
  @confluence
end

#encoreObject

Returns the value of attribute encore.



16
17
18
# File 'lib/confluence/confluence_remote_data_object.rb', line 16

def encore
  @encore
end

Class Method Details

.confluenceObject



28
29
30
31
# File 'lib/confluence/confluence_remote_data_object.rb', line 28

def self.confluence
  raise "Cannot establish confluence connection because the connector has not been set." unless @@connector
  @@connector.connect
end

.connectorObject



24
25
26
# File 'lib/confluence/confluence_remote_data_object.rb', line 24

def self.connector
  @@connector
end

.connector=(connector) ⇒ Object



20
21
22
# File 'lib/confluence/confluence_remote_data_object.rb', line 20

def self.connector=(connector)
  @@connector = connector
end

.encoreObject

TODO: encore-specific code like this probably shouldn’t be here…



39
40
41
42
# File 'lib/confluence/confluence_remote_data_object.rb', line 39

def self.encore
  raise "Cannot establish confluence connection because the connector has not been set." unless @@connector
	@@connector.connect("encore")
end

.find(id) ⇒ Object

class methods #########################################################



137
138
139
140
# File 'lib/confluence/confluence_remote_data_object.rb', line 137

def self.find(id)
  r = get(id)
  self.new(r)
end

Instance Method Details

#==(obj) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/confluence/confluence_remote_data_object.rb', line 127

def ==(obj)
  if obj.kind_of? self.class
    self.attributes == obj.attributes
  else
    super
  end
end

#[](attr) ⇒ Object



105
106
107
# File 'lib/confluence/confluence_remote_data_object.rb', line 105

def [](attr)
  self.attributes[attr]
end

#[]=(attr, value) ⇒ Object



109
110
111
# File 'lib/confluence/confluence_remote_data_object.rb', line 109

def []=(attr, value)
  self.attributes[attr] = value
end

#as_boolean(val) ⇒ Object



153
154
155
# File 'lib/confluence/confluence_remote_data_object.rb', line 153

def as_boolean(val)
  val == "true"
end

#as_datetime(val) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/confluence/confluence_remote_data_object.rb', line 157

def as_datetime(val)
  if val.is_a?(String) 
    # for older versions of Confluence (pre 2.7?)
    val =~ /\w{3} (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) (\w{3}) (\d{4})/
    month = $1
    day = $2
    hour = $3
    minute = $4
    second = $5
    tz = $6
    year = $7
    Time.local(year, month, day, hour, minute, second)
  else
    Time.local(val.year, val.month, val.day, val.hour, val.min, val.sec)
  end
end

#as_int(val) ⇒ Object

TODO: put these in a module?



145
146
147
# File 'lib/confluence/confluence_remote_data_object.rb', line 145

def as_int(val)
  val.to_i
end

#as_string(val) ⇒ Object



149
150
151
# File 'lib/confluence/confluence_remote_data_object.rb', line 149

def as_string(val)
  val.to_s
end

#destroyObject

Raises:

  • (NotImplementedError)


96
97
98
99
100
101
102
103
# File 'lib/confluence/confluence_remote_data_object.rb', line 96

def destroy
  before_destroy if respond_to? :before_destroy
  
  raise NotImplementedError.new("Can't call #{self}.destroy because no @@destroy_method is defined for this class") unless self.destroy_method
  eval "confluence.#{self.destroy_method}(self.id.to_s)"
  
  after_destroy if respond_to? :after_destroy
end

#idObject



113
114
115
# File 'lib/confluence/confluence_remote_data_object.rb', line 113

def id
  self[:id]
end

#load_from_object(data_object) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/confluence/confluence_remote_data_object.rb', line 54

def load_from_object(data_object)
  data_object.each do |attr, value|
    if self.class.attr_conversions.has_key? attr.to_sym
      value = self.send("as_#{attr_conversions[attr.to_sym]}", value)
    end
    self.send("#{attr}=", value)
  end
end

#reloadObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/confluence/confluence_remote_data_object.rb', line 82

def reload
  before_reload if respond_to? :before_reload
  
  if self.id
    self.load_from_object(self.class.send(:get, self.id))
  else
    # We don't have an ID, so try to use alternate method for reloading. (This is for newly created records that may not yet have an id assigned)
    raise NotImplementedError, "Can't reload this #{self.class} because it does not have an id and does not implement the reload_newly_created! method." unless self.respond_to? :reload_newly_created!
    self.reload_newly_created!
  end
  
  after_reload if respond_to? :after_reload
end

#saveObject

Raises:

  • (NotImplementedError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/confluence/confluence_remote_data_object.rb', line 63

def save
  before_save if respond_to? :before_save
  
  data = {} unless data
  
  self.attributes.each do |attr,value|
    data[attr.to_s] = value.to_s unless self.readonly_attrs.include? attr
  end
  
  raise NotImplementedError.new("Can't call #{self}.save because no @@save_method is defined for this class") unless self.save_method
  
  self.confluence.send(self.class.send(:save_method), data)
  
  # we need to reload because the version number has probably changed, we want the new ID, etc.
  reload
  
  after_save if respond_to? :after_save
end