Class: Cropio::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
Attributes
Defined in:
lib/cropio/resource/base.rb

Constant Summary collapse

Proxy =
Cropio::Connection::Proxy
Limit =
1000

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes

included

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cropio::Resource::Attributes

Class Method Details

.allObject



21
22
23
# File 'lib/cropio/resource/base.rb', line 21

def self.all
  to_instances(get_all_chunks)
end

.countObject



25
26
27
# File 'lib/cropio/resource/base.rb', line 25

def self.count
  all.count
end

.resource_nameObject



13
14
15
# File 'lib/cropio/resource/base.rb', line 13

def self.resource_name
  @resource_name ||= StringInflector.underscore(name.split('::').last)
end

.resources_nameObject



17
18
19
# File 'lib/cropio/resource/base.rb', line 17

def self.resources_name
  @resources_name ||= StringInflector.pluralize(resource_name)
end

.select(options = {}) ⇒ Object



29
30
# File 'lib/cropio/resource/base.rb', line 29

def self.select(options={})
end

Instance Method Details

#destroy?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/cropio/resource/base.rb', line 49

def destroy?
  if persisted?
    Proxy.delete("#{resources_name}/#{id}")
    @persisted = false
    true
  else
    raise 'Cropio record is not persisted!'
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/cropio/resource/base.rb', line 32

def persisted?
  if @persisted.nil?
    @persisted = false
  end

  @persisted
end

#saveObject



40
41
42
43
44
45
46
47
# File 'lib/cropio/resource/base.rb', line 40

def save
  self.attributes = if persisted?
    Proxy.patch("#{resources_name}/#{id}", attributes)
  else
    @persisted = true
    Proxy.post(resources_name, attributes)
  end
end