Class: OneEye::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON
Defined in:
lib/models/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



13
14
15
16
# File 'lib/models/base.rb', line 13

def initialize(attributes = {})
  self.attributes = attributes
  defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



76
77
78
79
80
81
82
# File 'lib/models/base.rb', line 76

def method_missing(name, *args, &block)
  if name[-1] == "="
    return instance_variable_set "@#{name[0..-2]}", args[0]
  else
    instance_variable_get "@#{name}"
  end
end

Class Method Details

.apiObject



9
10
11
# File 'lib/models/base.rb', line 9

def self.api
  OneEye::API.new
end

.create(attributes = {}) ⇒ Object



49
50
51
# File 'lib/models/base.rb', line 49

def self.create(attributes={})
  new(api.post(resources_url, attributes))
end

.find(attributes = {}) ⇒ Object



39
40
41
# File 'lib/models/base.rb', line 39

def self.find(attributes={})
  new(api.get(resources_url, attributes))
end

.where(attributes = {}) ⇒ Object



43
44
45
46
47
# File 'lib/models/base.rb', line 43

def self.where(attributes={})
  api.get(resources_url, attributes).map do |model|
    new model
  end
end

Instance Method Details

#attributesObject



31
32
33
34
35
36
37
# File 'lib/models/base.rb', line 31

def attributes
  instance_variables.inject Hash.new do |attributes, key|
    key = key.to_s.gsub(/\@/) { "" }
    attributes[key] = send key
    attributes
  end
end

#attributes=(attrs) ⇒ Object



26
27
28
29
# File 'lib/models/base.rb', line 26

def attributes=(attrs)
  make_array_convertable attrs if attrs.is_a? Array
  attrs.to_hash.each { |k, v| send "#{k}=", v }
end

#defaultsObject



18
19
20
21
22
23
24
# File 'lib/models/base.rb', line 18

def defaults
  "    Please define behavior in subclasses.\n    See: Template method pattern.\n    http://en.wikipedia.org/wiki/Template_method_pattern\n  eos\nend\n"

#deleteObject



63
64
65
# File 'lib/models/base.rb', line 63

def delete
  destroy
end

#destroyObject



59
60
61
# File 'lib/models/base.rb', line 59

def destroy
  api.delete resources_url, non_nil_attributes
end

#update(attributes = {}) ⇒ Object



53
54
55
56
57
# File 'lib/models/base.rb', line 53

def update(attributes={})
  self.attributes = attributes
  self.attributes = api.put(resources_url, non_nil_attributes)
  self
end