Module: RestfulObjects::ObjectBase

Defined in:
lib/restful_objects/object_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#is_serviceObject

Returns the value of attribute is_service.



5
6
7
# File 'lib/restful_objects/object_base.rb', line 5

def is_service
  @is_service
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/restful_objects/object_base.rb', line 5

def title
  @title
end

Instance Method Details

#decode_value(value, type) ⇒ Object



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
# File 'lib/restful_objects/object_base.rb', line 95

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 value == 'true'
        true
      elsif value == 'false'
        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



71
72
73
# File 'lib/restful_objects/object_base.rb', line 71

def deleted?
  @deleted
end

#encode_value(value, type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/restful_objects/object_base.rb', line 75

def encode_value(value, type)
  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
      raise "encode_value unsupported property type: #{type}"
  end
end

#generate_membersObject



57
58
59
60
61
62
63
# File 'lib/restful_objects/object_base.rb', line 57

def generate_members
  if is_service
    actions_members
  else
    properties_members.merge(collections_members.merge(actions_members))
  end
end

#get_representationObject



29
30
31
32
# File 'lib/restful_objects/object_base.rb', line 29

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


121
122
123
# File 'lib/restful_objects/object_base.rb', line 121

def get_self_link
  link_to(:self, "/objects/#{self.class.name}/#{self.object_id}", :object)
end

#initializeObject



7
8
9
10
11
12
13
14
15
# File 'lib/restful_objects/object_base.rb', line 7

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

#representationObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/restful_objects/object_base.rb', line 38

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_deleteObject



65
66
67
68
69
# File 'lib/restful_objects/object_base.rb', line 65

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_idObject



34
35
36
# File 'lib/restful_objects/object_base.rb', line 34

def rs_instance_id
  object_id
end

#rs_modelObject



21
22
23
# File 'lib/restful_objects/object_base.rb', line 21

def rs_model
  RestfulObjects::DomainModel.current
end

#rs_register_in_modelObject



17
18
19
# File 'lib/restful_objects/object_base.rb', line 17

def rs_register_in_model
  rs_model.objects.register(self) unless @is_service
end

#rs_typeObject



25
26
27
# File 'lib/restful_objects/object_base.rb', line 25

def rs_type
  rs_model.types[self.class.name]
end