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

attribute, attributes, defaults, define_base_client_methods, define_client_methods, has_summary, inherited, params_from, queryable_by, scoped_to_organization, scoped_to_space, to_many, to_many_relations, to_one, to_one_relations

Constructor Details

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

Returns a new instance of Model.



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

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

#guidObject

Returns the value of attribute guid.



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

def guid
  @guid
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



79
80
81
# File 'lib/cfoundry/v2/model.rb', line 79

def attribute_for_error(error)
  :base
end

#changed?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/cfoundry/v2/model.rb', line 43

def changed?
  !@changes.empty?
end

#createObject



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

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!’



85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/cfoundry/v2/model.rb', line 85

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
    elsif k.to_s.end_with?("_url")
    else
      payload[k] = v
    end
  end

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

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

  @diff.clear

  true
end

#deleteObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/cfoundry/v2/model.rb', line 137

def delete
  delete!
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



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

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)


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

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

#exists?Boolean

Returns:

  • (Boolean)


174
175
176
177
178
179
180
# File 'lib/cfoundry/v2/model.rb', line 174

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

#hashObject



192
193
194
# File 'lib/cfoundry/v2/model.rb', line 192

def hash
  @guid.hash
end

#inspectObject



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

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

#invalidate!Object



59
60
61
62
63
64
65
# File 'lib/cfoundry/v2/model.rb', line 59

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

#manifestObject



35
36
37
# File 'lib/cfoundry/v2/model.rb', line 35

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

#object_nameObject



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

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

#partial?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cfoundry/v2/model.rb', line 39

def partial?
  @partial
end

#persisted?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/cfoundry/v2/model.rb', line 170

def persisted?
  @guid && !@deleted
end

#plural_object_nameObject



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

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

#query_target(klass) ⇒ Object



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

def query_target(klass)
  self
end

#to_keyObject



166
167
168
# File 'lib/cfoundry/v2/model.rb', line 166

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

#to_paramObject



162
163
164
# File 'lib/cfoundry/v2/model.rb', line 162

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

#update!Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cfoundry/v2/model.rb', line 125

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

  @diff.clear

  true
end