Module: RestfulObjects::ObjectBase
- Defined in:
- lib/restful_objects/domain_model/mixins/object_base.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, property_name = '') ⇒ Object
- #generate_members ⇒ Object
- #get_property_rel_representation(property_name) ⇒ Object
- #get_representation ⇒ Object
- #get_self_link ⇒ Object
- #initialize ⇒ Object
- #put_properties_and_get_representation(json) ⇒ Object
- #representation(include_self_link = true) ⇒ Object
- #ro_absolute_url ⇒ Object
- #ro_relative_url ⇒ 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.
3 4 5 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 3 def is_service @is_service end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 3 def title @title end |
Instance Method Details
#decode_value(value, type) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 122 def decode_value(value, type) return nil if value.nil? case type when :string value.to_s when :int value.to_i when :bool if [true, 'true'].include?(value) true elsif [false, 'false'].include?(value) false else raise ArgumentError.new "invalid boolean value: #{value}" end 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
94 95 96 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 94 def deleted? @deleted end |
#encode_value(value, type, property_name = '') ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 98 def encode_value(value, type, property_name = '') return nil if value.nil? case type when :string value.to_s when :int value.to_i when :bool value.to_s when :decimal value.to_f when :date value.strftime('%Y-%m-%d') when :blob Base64.encode64(value).strip else if value.respond_to?(:get_property_rel_representation) value.get_property_rel_representation(property_name) else raise "encode_value unsupported property type: #{type}" end end end |
#generate_members ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 73 def generate_members if is_service actions_members else properties_members.merge(collections_members.merge(actions_members)) end end |
#get_property_rel_representation(property_name) ⇒ Object
81 82 83 84 85 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 81 def get_property_rel_representation(property_name) representation = link_to(:value, "/objects/#{self.class.name}/#{object_id}", :object, property: property_name) representation['title'] = @title representation end |
#get_representation ⇒ Object
27 28 29 30 31 32 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 27 def get_representation [200, { 'Content-Type' => "application/json;profile=\"urn:org.restfulobjects:repr-types/object\";x-ro-domain-type=\"#{rs_type.id}\"" }, representation.to_json] end |
#get_self_link ⇒ Object
156 157 158 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 156 def get_self_link link_to(:self, ro_relative_url, :object) end |
#initialize ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 5 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 |
#put_properties_and_get_representation(json) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 34 def put_properties_and_get_representation(json) properties = JSON.parse(json) properties.each do |property, container| raise 'property not exists' unless rs_model.types[self.class.name].properties.include?(property) raise 'read-only property' if rs_model.types[self.class.name].properties[property].read_only set_property_value(property, container['value']) on_after_update if respond_to?(:on_after_update) end [200, { 'Content-Type' => "application/json;profile=\"urn:org.restfulobjects:repr-types/object\";x-ro-domain-type=\"#{rs_type.id}\"" }, representation(false).to_json] end |
#representation(include_self_link = true) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 52 def representation(include_self_link = true) representation = { 'instanceId' => object_id.to_s, 'serviceId' => self.class.name, 'title' => title, 'members' => generate_members, 'links' => [ link_to(:described_by, "/domain-types/#{self.class.name}", :domain_type) ], 'extensions' => rs_type. } unless is_service representation.delete('serviceId') representation['links'] << link_to(:self, "/objects/#{self.class.name}/#{object_id}", :object) if include_self_link else representation.delete('instanceId') representation['links'] << link_to(:self, "/services/#{self.class.name}", :object) if include_self_link end representation end |
#ro_absolute_url ⇒ Object
152 153 154 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 152 def ro_absolute_url RestfulObjects::DomainModel.current.base_url + ro_relative_url end |
#ro_relative_url ⇒ Object
148 149 150 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 148 def ro_relative_url "/objects/#{self.class.name}/#{self.object_id}" end |
#rs_delete ⇒ Object
87 88 89 90 91 92 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 87 def rs_delete on_before_delete if respond_to?(:on_before_delete) @deleted = true on_after_delete if respond_to?(:on_after_delete) {}.to_json end |
#rs_instance_id ⇒ Object
48 49 50 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 48 def rs_instance_id object_id end |
#rs_model ⇒ Object
19 20 21 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 19 def rs_model RestfulObjects::DomainModel.current end |
#rs_register_in_model ⇒ Object
15 16 17 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 15 def rs_register_in_model rs_model.register_object(self) unless @is_service end |
#rs_type ⇒ Object
23 24 25 |
# File 'lib/restful_objects/domain_model/mixins/object_base.rb', line 23 def rs_type rs_model.types[self.class.name] end |