Class: CFoundry::V1::Model

Inherits:
Object
  • Object
show all
Extended by:
ModelMagic
Defined in:
lib/cfoundry/v1/model.rb

Direct Known Subclasses

App, ServiceInstance, User

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModelMagic

attribute, define_client_methods, read_locations, write_locations

Constructor Details

#initialize(guid, client, manifest = nil) ⇒ Model

Returns a new instance of Model.



27
28
29
30
31
32
# File 'lib/cfoundry/v1/model.rb', line 27

def initialize(guid, client, manifest = nil)
  @guid = guid
  @client = client
  @manifest = manifest
  @changes = {}
end

Class Attribute Details

.base_object_nameObject



20
21
22
# File 'lib/cfoundry/v1/model.rb', line 20

def base_object_name
  @base_object_name ||= object_name
end

.object_nameObject



13
14
15
16
17
18
# File 'lib/cfoundry/v1/model.rb', line 13

def object_name
  @object_name ||=
    name.split("::").last.gsub(
      /([a-z])([A-Z])/,
      '\1_\2').downcase.to_sym
end

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



25
26
27
# File 'lib/cfoundry/v1/model.rb', line 25

def changes
  @changes
end

#guidObject

Returns the value of attribute guid.



25
26
27
# File 'lib/cfoundry/v1/model.rb', line 25

def guid
  @guid
end

Instance Method Details

#base_object_nameObject



50
51
52
# File 'lib/cfoundry/v1/model.rb', line 50

def base_object_name
  @base_object_name ||= self.class.base_object_name
end

#changed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/cfoundry/v1/model.rb', line 38

def changed?
  !@changes.empty?
end

#create!Object



63
64
65
66
67
68
69
# File 'lib/cfoundry/v1/model.rb', line 63

def create!
  @manifest = @client.base.send(:"create_#{base_object_name}", write_manifest)

  @guid = read_manifest[guid_name]

  true
end

#delete!Object



79
80
81
82
83
84
85
# File 'lib/cfoundry/v1/model.rb', line 79

def delete!
  @client.base.send(:"delete_#{base_object_name}", @guid)

  @guid = nil

  true
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


144
145
146
# File 'lib/cfoundry/v1/model.rb', line 144

def eql?(other)
  other.is_a?(self.class) && @guid == other.guid
end

#exists?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/cfoundry/v1/model.rb', line 87

def exists?
  invalidate!
  manifest
  true
rescue CFoundry::NotFound
  false
end

#find_path(hash, path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cfoundry/v1/model.rb', line 108

def find_path(hash, path)
  return [false, nil] unless hash

  first, *rest = path
  return [false, nil] unless hash.key?(first)

  here = hash[first]

  if rest.empty?
    [true, here]
  else
    find_path(here, rest)
  end
end

#guid_nameObject



54
55
56
# File 'lib/cfoundry/v1/model.rb', line 54

def guid_name
  self.class.guid_name
end

#hashObject



149
150
151
# File 'lib/cfoundry/v1/model.rb', line 149

def hash
  @guid.hash
end

#inspectObject



42
43
44
# File 'lib/cfoundry/v1/model.rb', line 42

def inspect
  "\#<#{self.class.name} '#@guid'>"
end

#invalidate!Object



58
59
60
61
# File 'lib/cfoundry/v1/model.rb', line 58

def invalidate!
  @manifest = nil
  @changes = {}
end

#manifestObject



34
35
36
# File 'lib/cfoundry/v1/model.rb', line 34

def manifest
  @manifest ||= @client.base.send(base_object_name, @guid)
end

#object_nameObject



46
47
48
# File 'lib/cfoundry/v1/model.rb', line 46

def object_name
  @object_name ||= self.class.object_name
end

#put(what, where, path) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/cfoundry/v1/model.rb', line 133

def put(what, where, path)
  if path.size == 1
    where[path.last] = what
  elsif name = path.first
    where[name] ||= {}
    put(what, where[name], path[1..-1])
  end

  nil
end

#read_manifestObject



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cfoundry/v1/model.rb', line 95

def read_manifest
  read = {}

  self.class.read_locations.each do |name, where|
    found, val = find_path(manifest, where)
    read[name] = val if found
  end

  read[guid_name] = @guid

  read
end

#update!Object



71
72
73
74
75
76
77
# File 'lib/cfoundry/v1/model.rb', line 71

def update!
  @client.base.send(:"update_#{base_object_name}", @guid, write_manifest)

  invalidate!

  true
end

#write_manifest(body = read_manifest, onto = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/cfoundry/v1/model.rb', line 123

def write_manifest(body = read_manifest, onto = {})
  onto[guid_name] = @guid

  self.class.write_locations.each do |what, where|
    put(body[what], onto, where) if body.key?(what)
  end

  onto
end