Module: CFoundry::V1::ModelMagic
- Included in:
- Model
- Defined in:
- lib/cfoundry/v1/model_magic.rb
Instance Attribute Summary collapse
-
#guid_name ⇒ Object
Returns the value of attribute guid_name.
Instance Method Summary collapse
- #attribute(name, type, opts = {}) ⇒ Object
- #define_client_methods(klass = self) ⇒ Object
- #on_base_client(&blk) ⇒ Object
- #on_client(&blk) ⇒ Object
- #read_locations ⇒ Object
- #read_only_attributes ⇒ Object
- #write_locations ⇒ Object
Instance Attribute Details
#guid_name ⇒ Object
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
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 124 125 126 127 |
# File 'lib/cfoundry/v1/model_magic.rb', line 87 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) unless write_only write_locations[name] = Array(opts[:write] || opts[:at] || name) unless read_only read_only_attributes << name if read_only self.guid_name = name if is_guid define_method(name) do return @guid if @guid && is_guid read = read_manifest read.key?(name) ? read[name] : default end 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 write_to = read_only ? self.class.read_locations : self.class.write_locations put(val, @manifest, write_to[name]) end private name if write_only private :"#{name}=" if read_only end |
#define_client_methods(klass = self) ⇒ Object
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 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cfoundry/v1/model_magic.rb', line 33 def define_client_methods(klass = self) singular = klass.object_name plural = klass.plural_object_name base_singular = klass.base_object_name base_plural = klass.plural_base_object_name on_base_client do define_method(base_singular) do |guid| get(base_plural, guid, :accept => :json) end define_method(:"create_#{base_singular}") do |payload| post(base_plural, :content => :json, :accept => :json, :payload => payload) end define_method(:"delete_#{base_singular}") do |guid| delete(base_plural, guid) true end define_method(:"update_#{base_singular}") do |guid, payload| put(base_plural, guid, :content => :json, :payload => payload) end define_method(base_plural) do |*args| get(base_plural, :accept => :json) end end on_client 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| , _ = args ||= {} @base.send(base_plural).collect do |json| klass.new(json[klass.guid_name], self, json) end end end end |
#on_base_client(&blk) ⇒ Object
29 30 31 |
# File 'lib/cfoundry/v1/model_magic.rb', line 29 def on_base_client(&blk) BaseClientMethods.module_eval(&blk) end |
#on_client(&blk) ⇒ Object
25 26 27 |
# File 'lib/cfoundry/v1/model_magic.rb', line 25 def on_client(&blk) ClientMethods.module_eval(&blk) end |
#read_locations ⇒ Object
13 14 15 |
# File 'lib/cfoundry/v1/model_magic.rb', line 13 def read_locations @read_locations ||= {} end |
#read_only_attributes ⇒ Object
21 22 23 |
# File 'lib/cfoundry/v1/model_magic.rb', line 21 def read_only_attributes @read_only_attributes ||= [] end |
#write_locations ⇒ Object
17 18 19 |
# File 'lib/cfoundry/v1/model_magic.rb', line 17 def write_locations @write_locations ||= {} end |