Class: Rested::Entity

Inherits:
Base
  • Object
show all
Defined in:
lib/rested/entity.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseMethods

#client, #decode_json_response, #decode_response, #decode_xml_response, #delete, #get, #handle_error, #handle_response, #post, #setup_client

Constructor Details

#initialize(*args) ⇒ Entity

Returns a new instance of Entity.



108
109
110
111
112
113
114
115
116
# File 'lib/rested/entity.rb', line 108

def initialize(*args)
  if args.kind_of? Hash then
    h = args
  elsif args.kind_of? Array and args.first.kind_of? Hash then
    h = args.first
  end
  set_values(h) if h
  self.errors = []
end

Class Attribute Details

.after_filtersObject (readonly)

Returns the value of attribute after_filters.



14
15
16
# File 'lib/rested/entity.rb', line 14

def after_filters
  @after_filters
end

.before_filtersObject (readonly)

Returns the value of attribute before_filters.



14
15
16
# File 'lib/rested/entity.rb', line 14

def before_filters
  @before_filters
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



9
10
11
# File 'lib/rested/entity.rb', line 9

def errors
  @errors
end

Class Method Details

.add_field(field) ⇒ Object



66
67
68
69
# File 'lib/rested/entity.rb', line 66

def add_field(field)
  self.fields << field
  attr_accessor field
end

.after_save(&block) ⇒ Object



28
29
30
# File 'lib/rested/entity.rb', line 28

def after_save(&block)
  after_filters << block
end

.before_save(&block) ⇒ Object



24
25
26
# File 'lib/rested/entity.rb', line 24

def before_save(&block)
  before_filters << block
end

.delimited_field(field, delimiter = ',') ⇒ Object



59
60
61
62
63
64
# File 'lib/rested/entity.rb', line 59

def delimited_field(field, delimiter = ',')
  unless fields.include? field
    delimited_fields[field] = delimiter
    add_field(field)
  end
end

.delimited_fieldsObject



55
56
57
# File 'lib/rested/entity.rb', line 55

def delimited_fields
  @delimited_fields ||= {}
end

.field(*args) ⇒ Object



49
50
51
52
53
# File 'lib/rested/entity.rb', line 49

def field(*args)
  args.each do |f|
    add_field(f) unless fields.include? f 
  end
end

.fieldsObject



45
46
47
# File 'lib/rested/entity.rb', line 45

def fields
  @fields ||= inherit_static_from_super(:fields) || []
end

.find(id = nil, masquerade = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rested/entity.rb', line 71

def find(id = nil, masquerade = nil)
  uri = self.endpoint
  uri += "/#{id}" if not id.nil?
  begin
    json = get(uri, :masquerade => masquerade)
  rescue Rested::Error => ex
    if ex.message =~ /Invalid/ then
      raise ObjectNotFound.new(ex.http_response)
    end
  end
  if id.nil? then
    # return as list
    return json.values.first.map { |j| new(j) }
  end
  return nil if json.values.empty?
  return new(json.values.first)
end

.id_fieldObject



32
33
34
# File 'lib/rested/entity.rb', line 32

def id_field
  @id_field ||= inherit_static_from_super(:id_field) || nil
end

.inherit_static_from_super(sym) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rested/entity.rb', line 36

def inherit_static_from_super(sym)
  if superclass.respond_to? sym then
    val = superclass.send(sym)
    return val if val.kind_of? Symbol
    return val.dup if not val.nil?
  end
  return nil
end

.listObject



89
90
91
# File 'lib/rested/entity.rb', line 89

def list
  find()
end

Instance Method Details

#[](name) ⇒ Object



158
159
160
161
162
163
164
# File 'lib/rested/entity.rb', line 158

def [](name)
  begin
    send(name)
  rescue NoMethodError
    nil
  end
end

#[]=(name, value) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/rested/entity.rb', line 166

def []=(name, value)
  begin
    send("#{name}=", value)
  rescue NoMethodError
    self.class.add_field(name)
    send("#{name}=", value)
  end
end

#add_file(name, file) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rested/entity.rb', line 99

def add_file(name, file)
  if file.kind_of? String then
    raise IOError.new("File not found: #{file}") if not File.exists? file
    file = File.new(file)
  end
  return if not (file.respond_to?(:read) and file.respond_to?(:pos) and file.respond_to?(:pos=))
  self.files[name] = file
end

#delete!Object



242
243
244
245
246
247
# File 'lib/rested/entity.rb', line 242

def delete!
  return if new?
  uri = self.endpoint + "/#{self.id_val}"
  self.delete(uri)
  true
end

#delimited_fieldsObject



253
254
255
# File 'lib/rested/entity.rb', line 253

def delimited_fields
  @delimited_fields ||= self.class.delimited_fields
end

#fieldsObject



249
250
251
# File 'lib/rested/entity.rb', line 249

def fields
  @fields ||= self.class.fields
end

#filesObject



95
96
97
# File 'lib/rested/entity.rb', line 95

def files
  @files ||= {}
end

#id_valObject



142
143
144
# File 'lib/rested/entity.rb', line 142

def id_val
  self.send(id_field)
end

#id_val=(val) ⇒ Object



146
147
148
# File 'lib/rested/entity.rb', line 146

def id_val=(val)
  self.send("#{id_field.to_s}=", val)
end

#new?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/rested/entity.rb', line 154

def new?
  self.id_val.nil?
end

#new_record?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/rested/entity.rb', line 150

def new_record?
  self.new?
end

#parse_value(name, value) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rested/entity.rb', line 130

def parse_value(name, value)
  if !value.nil? then
    if delimited_fields.include?(name.to_sym) then
      value = value.split(delimited_fields[name.to_sym]) if value.is_a?(String)
      value = value.map(&:to_i) if value.first.is_a?(String) && value.all?{ |v| v.to_i.to_s == v }
    else
      value = value.to_i if value.is_a?(String) && value.to_i.to_s == value
    end
    value
  end
end

#saveObject



199
200
201
202
203
204
205
206
# File 'lib/rested/entity.rb', line 199

def save
  begin
    save!
  rescue Rested::Error => e
    self.errors = e.validations
    false
  end
end

#save!(params = nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rested/entity.rb', line 208

def save!(params = nil)
  self.class.before_filters.each do |f| 
    f.call(self) 
  end 
  
  uri = self.endpoint
  uri += "/#{self.id_val}" unless new?
  
  params = to_h() if not params
  params.delete(self.id_field) if new?
  
  if not self.files.empty?
    params.merge!(self.files)
    @files = {}
  end
  params = preserve_filenames(params)
  
  begin      
    ret = self.post(uri, params)
    cleanup_temp_files(params)
  rescue => ex
    cleanup_temp_files(params)
    raise ex
  end
  
  if new? then
    self.id_val = self.class.new(ret.values.first).id_val
  end
  self.class.after_filters.each do |f| 
    f.call(self) 
  end 
  true
end

#set_values(h) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rested/entity.rb', line 118

def set_values(h)
  h.each_pair do |name, value|
    writer_method = "#{name}="
    value = parse_value(name, value)
    if respond_to?(writer_method)
      send(writer_method, value)
    else
      self[name.to_s] = value
    end
  end
end

#to_hObject



175
176
177
178
179
180
181
182
# File 'lib/rested/entity.rb', line 175

def to_h
  h = {}
  fields.each do |f|
    val = self.send(f)
    h[f] = val && delimited_fields.include?(f) ? val.join(delimited_fields[f]) : val
  end
  h
end

#to_jsonObject



184
185
186
# File 'lib/rested/entity.rb', line 184

def to_json
  JSON.generate(to_h())
end

#to_sObject



188
189
190
# File 'lib/rested/entity.rb', line 188

def to_s
  to_json()
end

#update_attributes(attributes = {}) ⇒ Object



192
193
194
195
196
197
# File 'lib/rested/entity.rb', line 192

def update_attributes(attributes = {})
  attributes.each_pair do |field, value|
    send("#{field}=", value)
  end
  save
end