Module: RestfulObjects::ObjectBase
- Defined in:
- lib/restful_objects/object.rb
Instance Attribute Summary collapse
-
#is_service ⇒ Object
Returns the value of attribute is_service.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #decode_value(value, type) ⇒ Object
- #deleted? ⇒ Boolean
- #encode_value(value, type) ⇒ Object
- #generate_members ⇒ Object
- #get_representation ⇒ Object
- #get_self_link ⇒ Object
- #initialize ⇒ Object
- #representation ⇒ Object
- #rs_delete ⇒ Object
- #rs_instance_id ⇒ Object
- #rs_model ⇒ Object
- #rs_register_in_model ⇒ Object
- #rs_type ⇒ Object
Instance Attribute Details
#is_service ⇒ Object
Returns the value of attribute is_service.
22 23 24 |
# File 'lib/restful_objects/object.rb', line 22 def is_service @is_service end |
#title ⇒ Object
Returns the value of attribute title.
22 23 24 |
# File 'lib/restful_objects/object.rb', line 22 def title @title end |
Instance Method Details
#decode_value(value, type) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/restful_objects/object.rb', line 110 def decode_value(value, type) return nil if value.nil? case type when :string value.to_s when :int value.to_i when :decimal Float(value) when :date Date.parse(value) when :blob Base64.decode64(value) else raise "decode_value unsupported property type: #{type}" end end |
#deleted? ⇒ Boolean
88 89 90 |
# File 'lib/restful_objects/object.rb', line 88 def deleted? @deleted end |
#encode_value(value, type) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/restful_objects/object.rb', line 92 def encode_value(value, type) return nil if value.nil? case type when :string value.to_s when :int value.to_i when :decimal value.to_f when :date value.strftime('%Y-%m-%d') when :blob Base64.encode64(value).strip else raise "encode_value unsupported property type: #{type}" end end |
#generate_members ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/restful_objects/object.rb', line 74 def generate_members if is_service actions_members else properties_members.merge(collections_members.merge(actions_members)) end end |
#get_representation ⇒ Object
46 47 48 49 |
# File 'lib/restful_objects/object.rb', line 46 def get_representation HttpResponse.new(representation.to_json, 'application/json;profile="urn:org.restfulobjects:repr-types/object";x-ro-domain-type="' + rs_type.id + '"') end |
#get_self_link ⇒ Object
128 129 130 |
# File 'lib/restful_objects/object.rb', line 128 def get_self_link link_to(:self, "/objects/#{self.class.name}/#{self.object_id}", :object) end |
#initialize ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/restful_objects/object.rb', line 24 def initialize super @deleted = false @is_service = self.class.ancestors.include? RestfulObjects::Service @title = "#{self.class.name} (#{object_id})" rs_register_in_model rs_type.collections.each_value { |collection| instance_variable_set "@#{collection.id}".to_sym, Array.new } on_after_create if respond_to? :on_after_create end |
#representation ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/restful_objects/object.rb', line 55 def representation representation = { 'title' => title, 'members' => generate_members, 'links' => [ link_to(:described_by, "/domain-types/#{self.class.name}", :domain_type) ], 'extensions' => rs_type. } if not is_service representation['instanceId'] = object_id.to_s representation['links'] << link_to(:self, "/objects/#{self.class.name}/#{object_id}", :object) else representation['serviceId'] = self.class.name representation['links'] << link_to(:self, "/services/#{self.class.name}", :object) end representation end |
#rs_delete ⇒ Object
82 83 84 85 86 |
# File 'lib/restful_objects/object.rb', line 82 def rs_delete on_before_delete if respond_to? :on_before_delete @deleted = true on_after_delete if respond_to? :on_after_delete end |
#rs_instance_id ⇒ Object
51 52 53 |
# File 'lib/restful_objects/object.rb', line 51 def rs_instance_id object_id end |
#rs_model ⇒ Object
38 39 40 |
# File 'lib/restful_objects/object.rb', line 38 def rs_model RestfulObjects::DomainModel.current end |
#rs_register_in_model ⇒ Object
34 35 36 |
# File 'lib/restful_objects/object.rb', line 34 def rs_register_in_model rs_model.objects.register(self) if not @is_service end |
#rs_type ⇒ Object
42 43 44 |
# File 'lib/restful_objects/object.rb', line 42 def rs_type rs_model.types[self.class.name] end |