Class: CFoundry::V2::Model

Inherits:
Object
  • Object
show all
Extended by:
ModelMagic
Includes:
ActiveModel::Validations
Defined in:
lib/cfoundry/v2/model.rb

Constant Summary collapse

@@objects =
{}

Instance Attribute Summary collapse

Attributes included from ModelMagic

#scoped_organization, #scoped_space

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModelMagic

attributes, defaults, inherited, params_from, query_from_hash, scoped_to_organization, scoped_to_space, to_many_relations, to_one_relations

Methods included from CFoundry::V2::ModelMagic::QueryableBy

#queryable_by

Methods included from CFoundry::V2::ModelMagic::ToMany

#to_many

Methods included from CFoundry::V2::ModelMagic::ToOne

#to_one

Methods included from CFoundry::V2::ModelMagic::Attribute

#attribute

Methods included from CFoundry::V2::ModelMagic::HasSummary

#has_summary

Methods included from CFoundry::V2::ModelMagic::ClientExtensions

#add_client_methods

Constructor Details

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

Returns a new instance of Model.



27
28
29
30
31
32
33
34
35
# File 'lib/cfoundry/v2/model.rb', line 27

def initialize(guid, client, manifest = nil, partial = false)
  @guid = guid
  @client = client
  @manifest = manifest
  @partial = partial
  @cache = {}
  @diff = {}
  @changes = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



23
24
25
# File 'lib/cfoundry/v2/model.rb', line 23

def cache
  @cache
end

#changesObject

Returns the value of attribute changes.



23
24
25
# File 'lib/cfoundry/v2/model.rb', line 23

def changes
  @changes
end

#created_atObject (readonly)

Returns the value of attribute created_at.



24
25
26
# File 'lib/cfoundry/v2/model.rb', line 24

def created_at
  @created_at
end

#diffObject (readonly)

Returns the value of attribute diff.



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

def diff
  @diff
end

#guidObject

Returns the value of attribute guid.



23
24
25
# File 'lib/cfoundry/v2/model.rb', line 23

def guid
  @guid
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



24
25
26
# File 'lib/cfoundry/v2/model.rb', line 24

def updated_at
  @updated_at
end

Class Method Details

.inherited(klass) ⇒ Object



17
18
19
20
# File 'lib/cfoundry/v2/model.rb', line 17

def inherited(klass)
  @@objects[klass.object_name] = klass
  super
end

.objectsObject



13
14
15
# File 'lib/cfoundry/v2/model.rb', line 13

def objects
  @@objects
end

Instance Method Details

#attribute_for_error(error) ⇒ Object



91
92
93
# File 'lib/cfoundry/v2/model.rb', line 91

def attribute_for_error(error)
  :base
end

#changed?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cfoundry/v2/model.rb', line 55

def changed?
  !@changes.empty?
end

#createObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cfoundry/v2/model.rb', line 79

def create
  create!
  true
rescue CFoundry::APIError => e
  if e.instance_of? CFoundry::APIError
    errors.add(:base, :cc_client)
  else
    errors.add(attribute_for_error(e), e.message)
  end
  false
end

#create!Object

this does a bit of extra processing to allow for ‘delete!’ followed by ‘create!’



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/cfoundry/v2/model.rb', line 97

def create!
  payload = {}

  @manifest ||= {}
  @manifest[:entity] ||= {}

  @manifest[:entity].each do |k, v|
    if v.is_a?(Hash) && v.key?(:metadata)
      # skip; there's a _guid attribute already
    elsif v.is_a?(Array) && !v.empty? && v.all? { |x|
      x.is_a?(Hash) && x.key?(:metadata)
    }
      singular = k.to_s.sub(/s$/, "")

      payload[:"#{singular}_guids"] = v.collect do |x|
        if x.is_a?(Hash) && x.key?(:metadata)
          x[:metadata][:guid]
        else
          x
        end
      end
    else
      payload[k] = v
    end
  end

  @manifest = @client.base.post("v2", create_endpoint_name,
    :content => :json,
    :accept => :json,
    :payload => payload
  )

  @guid = @manifest[:metadata][:guid]

  @diff.clear

  true
end

#create_endpoint_nameObject



136
137
138
# File 'lib/cfoundry/v2/model.rb', line 136

def create_endpoint_name
  plural_object_name
end

#delete(options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/cfoundry/v2/model.rb', line 152

def delete(options = {})
  delete!(options)
rescue CFoundry::APIError => e
  if e.instance_of? CFoundry::APIError
    errors.add(:base, :cc_client)
  else
    errors.add(attribute_for_error(e), e.message)
  end
  false
end

#delete!(options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cfoundry/v2/model.rb', line 163

def delete!(options = {})
  @client.base.delete("v2", plural_object_name, guid, :params => options)

  @deleted = true

  @diff.clear

  if @manifest
    @manifest.delete :metadata
  end

  true
end

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

Returns:

  • (Boolean)


201
202
203
# File 'lib/cfoundry/v2/model.rb', line 201

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

#exists?Boolean

Returns:

  • (Boolean)


189
190
191
192
193
194
195
# File 'lib/cfoundry/v2/model.rb', line 189

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

#hashObject



207
208
209
# File 'lib/cfoundry/v2/model.rb', line 207

def hash
  @guid.hash
end

#inspectObject



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

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

#invalidate!Object



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

def invalidate!
  @manifest = nil
  @partial = false
  @cache = {}
  @diff = {}
  @changes = {}
end

#manifestObject



47
48
49
# File 'lib/cfoundry/v2/model.rb', line 47

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

#object_nameObject



63
64
65
# File 'lib/cfoundry/v2/model.rb', line 63

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

#partial?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/cfoundry/v2/model.rb', line 51

def partial?
  @partial
end

#persisted?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/cfoundry/v2/model.rb', line 185

def persisted?
  @guid && !@deleted
end

#plural_object_nameObject



67
68
69
# File 'lib/cfoundry/v2/model.rb', line 67

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

#query_target(klass) ⇒ Object



197
198
199
# File 'lib/cfoundry/v2/model.rb', line 197

def query_target(klass)
  self
end

#to_keyObject



181
182
183
# File 'lib/cfoundry/v2/model.rb', line 181

def to_key
  persisted? ? [@guid] : nil
end

#to_paramObject



177
178
179
# File 'lib/cfoundry/v2/model.rb', line 177

def to_param
  persisted? ? @guid.to_s : nil
end

#update!Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cfoundry/v2/model.rb', line 140

def update!
  @manifest = @client.base.put("v2", plural_object_name, guid,
    :content => :json,
    :accept => :json,
    :payload => @diff
  )

  @diff.clear

  true
end