Class: ApphoshiesClient::Document

Inherits:
Base
  • Object
show all
Defined in:
lib/apphoshies_client/document.rb

Direct Known Subclasses

ContentItem, TrackingItem, User

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

find_by_application_client_key, find_by_datasource, find_one, get, reload_http_headers

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/apphoshies_client/document.rb', line 40

def method_missing(m, *args, &block)
  unless m.to_s == ('values')
    _object = get_value(m.to_s)
    return _object if _object
  end
  begin
    super(m, *args, &block)
  rescue => e
    return nil
  end
end

Class Method Details

.all(options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/apphoshies_client/document.rb', line 7

def self.all(options = {})
  if options and options[:datasource]
    find_by_datasource(options[:datasource], options)
  else
    raise ApphoshiesClient::MissingDatasourceException, 'Please provide a datasource option!'
  end
end

Instance Method Details

#get_value(key) ⇒ Object



15
16
17
18
19
20
# File 'lib/apphoshies_client/document.rb', line 15

def get_value(key)
  if self.values
    v = self.values.select {|value_object| value_object.respond_to?(key.to_sym)}
    return v.first.send(key.to_sym) if v and v.any?
  end
end

#set_value(key, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/apphoshies_client/document.rb', line 22

def set_value(key, value)
  _values = self.values.collect {|v| v.attributes}
  if value.is_a?(String)
    _values << {key => value, "type" => value.class.name.to_s}
  elsif value.is_a?(Fixnum)
    _values << {key => value, "type" => 'Integer'}
  elsif value.is_a?(Float)
    _values << {key => value, "type" => 'Float'}
  elsif value.is_a?(TrueClass) or value.is_a?(FalseClass)
    _values << {key => value, "type" => 'Boolean'}
  elsif value.is_a?(DateTime)
    _values << {key => value, "type" => 'DateTime'}
  else
    raise ApphoshiesClient::ValueTypeNotSupportedException
  end
  self.values = _values
end