Class: CFoundry::V2::Model

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

Instance Attribute Summary collapse

Attributes included from ModelMagic

#scoped_organization, #scoped_space

Instance Method Summary collapse

Methods included from ModelMagic

attribute, attributes, defaults, has_summary, inherited, params_from, queryable_by, scoped_to_organization, scoped_to_space, to_many, to_many_relations, to_one, to_one_relations, validate_type, value_matches?

Constructor Details

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

Returns a new instance of Model.



12
13
14
15
16
17
18
19
20
# File 'lib/cfoundry/v2/model.rb', line 12

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.



10
11
12
# File 'lib/cfoundry/v2/model.rb', line 10

def cache
  @cache
end

#changesObject

Returns the value of attribute changes.



10
11
12
# File 'lib/cfoundry/v2/model.rb', line 10

def changes
  @changes
end

#guidObject

Returns the value of attribute guid.



10
11
12
# File 'lib/cfoundry/v2/model.rb', line 10

def guid
  @guid
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/cfoundry/v2/model.rb', line 30

def changed?
  !@changes.empty?
end

#create!Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cfoundry/v2/model.rb', line 52

def create!
  payload = {}

  self.class.defaults.merge(@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.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?("_json") && v.is_a?(String)
      payload[k] = MultiJson.load(v)
    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



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

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)


120
121
122
# File 'lib/cfoundry/v2/model.rb', line 120

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

#exists?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
# File 'lib/cfoundry/v2/model.rb', line 109

def exists?
  @client.base.send(object_name, @guid)
  true
rescue CFoundry::NotFound
  false
end

#hashObject



125
126
127
# File 'lib/cfoundry/v2/model.rb', line 125

def hash
  @guid.hash
end

#inspectObject



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

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

#invalidate!Object



42
43
44
45
46
47
48
# File 'lib/cfoundry/v2/model.rb', line 42

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

#manifestObject



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

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

#object_nameObject



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

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

#partial?Boolean

Returns:

  • (Boolean)


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

def partial?
  @partial
end

#query_target(klass) ⇒ Object



116
117
118
# File 'lib/cfoundry/v2/model.rb', line 116

def query_target(klass)
  self
end

#update!Object



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

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

  @diff.clear

  true
end