Class: Cropio::Resource::Base

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

Overview

Represents ActiveRecord::Base-like model’s class with Cropio data selection and mutation.

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.



11
12
13
# File 'lib/cropio/resource/base.rb', line 11

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

Get all resources.



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

def self.all
  to_instances(get_all_chunks)
end

.changes(from_time = nil, to_time = nil) ⇒ Object



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

def self.changes(from_time = nil, to_time = nil)
  from_time = to_datetime_if_string(from_time)
  to_time = to_datetime_if_string(to_time)
  to_instances(get_all_changes(from_time, to_time))
end

.countObject

Count all resources.



31
32
33
# File 'lib/cropio/resource/base.rb', line 31

def self.count
  all.count
end

.resource_nameObject

Returns name of Resource



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

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

.resources_nameObject

Return pluralized version of Resource’s name



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

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

Instance Method Details

#destroyObject

Remove this resource from Cropio.



61
62
63
64
65
66
67
68
69
# File 'lib/cropio/resource/base.rb', line 61

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

#persisted?Boolean

Returns persistance of the resource. Resource is persisted if it is saved and not deleted, if this resource exists on Cropio servers.

Returns:

  • (Boolean)


45
46
47
# File 'lib/cropio/resource/base.rb', line 45

def persisted?
  !@persisted.nil? && (@persisted ||= false)
end

#saveObject

Saves current resource to Cropio.



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

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