Class: CFoundry::V2::Model

Inherits:
Object
  • Object
show all
Extended by:
ModelMagic
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

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  !@changes.empty?
end

#create!Object

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cfoundry/v2/model.rb', line 69

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.send(:"create_#{object_name}", payload)

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

  @diff.clear

  true
end

#delete!Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cfoundry/v2/model.rb', line 113

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

  @guid = nil

  @diff.clear

  if @manifest
    @manifest.delete :metadata
  end

  true
end

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

Returns:

  • (Boolean)


139
140
141
# File 'lib/cfoundry/v2/model.rb', line 139

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

#exists?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#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



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

def query_target(klass)
  self
end

#update!Object



105
106
107
108
109
110
111
# File 'lib/cfoundry/v2/model.rb', line 105

def update!
  @manifest = @client.base.send(:"update_#{object_name}", @guid, @diff)

  @diff.clear

  true
end