Class: Cocupu::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/cocupu/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Node

Returns a new instance of Node.



5
6
7
8
# File 'lib/cocupu/node.rb', line 5

def initialize(values)
  @conn = Thread.current[:cocupu_connection] 
  self.values = values
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/cocupu/node.rb', line 4

def conn
  @conn
end

#valuesObject

Returns the value of attribute values.



3
4
5
# File 'lib/cocupu/node.rb', line 3

def values
  @values
end

Instance Method Details

#associations=(associations) ⇒ Object



34
35
36
# File 'lib/cocupu/node.rb', line 34

def associations=(associations)
  values['associations'] = associations
end

#attach_file(file_name, file) ⇒ Object



56
57
58
59
60
# File 'lib/cocupu/node.rb', line 56

def attach_file(file_name, file)
  raise RuntimeError "You can't attach a file to an object that hasn't been persisted" unless persistent_id
  Cocupu::File.new(self, file_name, file)

end

#identityObject



14
15
16
# File 'lib/cocupu/node.rb', line 14

def identity
  values['identity']
end

#model_idObject



10
11
12
# File 'lib/cocupu/node.rb', line 10

def model_id
  values['model_id']
end

#persistent_idObject



38
39
40
# File 'lib/cocupu/node.rb', line 38

def persistent_id
  values['persistent_id']
end

#persistent_id=(id) ⇒ Object



30
31
32
# File 'lib/cocupu/node.rb', line 30

def persistent_id=(id)
  values['persistent_id'] = id
end

#poolObject



18
19
20
# File 'lib/cocupu/node.rb', line 18

def pool
  values['pool']
end

#saveObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cocupu/node.rb', line 42

def save
  response = if persistent_id
    conn.put("#{url}.json", body: {node: values})
  else
    conn.post("#{url}.json", body: {node: values})
  end
  raise "Error saving models: #{response.inspect}" unless response.code >= 200 and response.code < 300
  if (response['persistent_id'])
    self.persistent_id = response['persistent_id']
    self.url = response['url']
  end
  values
end

#urlObject



22
23
24
# File 'lib/cocupu/node.rb', line 22

def url
  values['url'] || "/#{identity}/#{pool}/nodes"
end

#url=(url) ⇒ Object



26
27
28
# File 'lib/cocupu/node.rb', line 26

def url=(url)
  values['url'] = url
end