Module: CFoundry::V1::ModelMagic

Included in:
Model
Defined in:
lib/cfoundry/v1/model_magic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#guid_nameObject

Returns the value of attribute guid_name.



11
12
13
# File 'lib/cfoundry/v1/model_magic.rb', line 11

def guid_name
  @guid_name
end

Instance Method Details

#attribute(name, type, opts = {}) ⇒ Object



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
104
105
106
107
108
109
110
111
112
# File 'lib/cfoundry/v1/model_magic.rb', line 75

def attribute(name, type, opts = {})
  default = opts[:default]
  is_guid = opts[:guid]
  read_only = opts[:read_only]
  write_only = opts[:write_only]
  has_default = opts.key?(:default)

  read_locations[name] = Array(opts[:read] || opts[:at] || name)
  write_locations[name] = Array(opts[:write] || opts[:at] || name)

  self.guid_name = name if is_guid

  unless write_only
    define_method(name) do
      return @guid if @guid && is_guid

      read = read_manifest
      read.key?(name) ? read[name] : default
    end
  end

  return if read_only

  define_method(:"#{name}=") do |val|
    unless has_default && val == default
      CFoundry::Validator.validate_type(val, type)
    end

    @guid = val if is_guid

    @manifest ||= {}

    old = read_manifest[name]
    @changes[name] = [old, val] if old != val

    put(val, @manifest, self.class.write_locations[name])
  end
end

#define_client_methods(klass = self) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cfoundry/v1/model_magic.rb', line 21

def define_client_methods(klass = self)
  singular = klass.object_name
  plural = :"#{singular}s"

  base_singular = klass.base_object_name
  base_plural = :"#{base_singular}s"

  BaseClientMethods.module_eval do
    define_method(base_singular) do |guid|
      get(base_plural, guid, :accept => :json)
    end

    define_method(:"create_#{base_singular}") do |payload|
      post(payload, base_plural, :content => :json, :accept => :json)
    end

    define_method(:"delete_#{base_singular}") do |guid|
      delete(base_plural, guid)
      true
    end

    define_method(:"update_#{base_singular}") do |guid, payload|
      put(payload, base_plural, guid, :content => :json)
    end

    define_method(base_plural) do |*args|
      get(base_plural, :accept => :json)
    end
  end

  ClientMethods.module_eval do
    if klass.guid_name
      define_method(:"#{singular}_by_#{klass.guid_name}") do |guid|
        obj = send(singular, guid)
        obj if obj.exists?
      end
    end

    define_method(singular) do |*args|
      guid, _ = args
      klass.new(guid, self)
    end

    define_method(plural) do |*args|
      options, _ = args
      options ||= {}

      @base.send(base_plural).collect do |json|
        klass.new(json[klass.guid_name], self, json)
      end
    end
  end
end

#read_locationsObject



13
14
15
# File 'lib/cfoundry/v1/model_magic.rb', line 13

def read_locations
  @read_locations ||= {}
end

#write_locationsObject



17
18
19
# File 'lib/cfoundry/v1/model_magic.rb', line 17

def write_locations
  @write_locations ||= {}
end