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.

Direct Known Subclasses

Cropio::Resources::AdditionalObject, Cropio::Resources::AgriWorkPlan, Cropio::Resources::AgriWorkPlanApplicationMixItem, Cropio::Resources::AgroOperation, Cropio::Resources::Alert, Cropio::Resources::AlertType, Cropio::Resources::ApplicationMixItem, Cropio::Resources::AutomaticAlert, Cropio::Resources::Avatar, Cropio::Resources::Chemical, Cropio::Resources::Counterparty, Cropio::Resources::Crop, Cropio::Resources::Fertilizer, Cropio::Resources::Field, Cropio::Resources::FieldGroup, Cropio::Resources::FieldScoutReport, Cropio::Resources::FieldScoutReportThreatMappingItem, Cropio::Resources::FieldShape, Cropio::Resources::FieldShapeLandParcelMappingItem, Cropio::Resources::FuelHourlyDataItem, Cropio::Resources::FuelMovement, Cropio::Resources::FuelPump, Cropio::Resources::FuelStation, Cropio::Resources::FuelTank, Cropio::Resources::FuelType, Cropio::Resources::GroupFolder, Cropio::Resources::GrowthScale, Cropio::Resources::GrowthStage, Cropio::Resources::GrowthStageGroup, Cropio::Resources::GrowthStageStructureMappingItem, Cropio::Resources::GrowthStagesPrediction, Cropio::Resources::HarvestWeighing, Cropio::Resources::HistoricalValue, Cropio::Resources::HistoryItem, Cropio::Resources::Implement, Cropio::Resources::ImplementRegionMappingItem, Cropio::Resources::InventoryHistoryItem, Cropio::Resources::LandDocument, Cropio::Resources::LandDocumentLandParcelMappingItem, Cropio::Resources::LandParcel, Cropio::Resources::Machine, Cropio::Resources::MachineGroup, Cropio::Resources::MachineRegion, Cropio::Resources::MachineRegionMappingItem, Cropio::Resources::MachineTask, Cropio::Resources::MachineTaskAgroOperationMappingItem, Cropio::Resources::MachineTaskFieldMappingItem, Cropio::Resources::MaintenancePlan, Cropio::Resources::MaintenancePlanRow, Cropio::Resources::MaintenancePlanRowSparePartMappingItem, Cropio::Resources::MaintenanceRecord, Cropio::Resources::MaintenanceRecordRow, Cropio::Resources::MaintenanceRecordRowSparePartMappingItem, Cropio::Resources::MaintenanceType, Cropio::Resources::MaintenanceTypeGroup, Cropio::Resources::Note, Cropio::Resources::Photo, Cropio::Resources::PlantThreat, Cropio::Resources::ProductivityEstimate, Cropio::Resources::ProductivityEstimateHistory, Cropio::Resources::ProductivityEstimatePeer, Cropio::Resources::ProtectedDocument, Cropio::Resources::SatelliteImage, Cropio::Resources::ScoutReportPoint, Cropio::Resources::ScoutReportPointGrowthStageStructure, Cropio::Resources::ScoutReportPointIssue, Cropio::Resources::ScoutReportPointIssuePlantPart, Cropio::Resources::ScoutReportPointMeasurement, Cropio::Resources::ScoutReportTemplate, Cropio::Resources::ScoutingTask, Cropio::Resources::ScoutingTaskPoint, Cropio::Resources::Seed, Cropio::Resources::SoilTest, Cropio::Resources::SoilTestSample, Cropio::Resources::SparePart, Cropio::Resources::SparePartManufacturer, Cropio::Resources::User, Cropio::Resources::UserRole, Cropio::Resources::UserRoleAssignment, Cropio::Resources::UserRolePermission, Cropio::Resources::Version, Cropio::Resources::WeatherHistoryItem, Cropio::Resources::WorkRecord, Cropio::Resources::WorkRecordMachineRegionMappingItem, Cropio::Resources::WorkType, Cropio::Resources::WorkTypeGroup

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



39
40
41
42
43
# File 'lib/cropio/resource/base.rb', line 39

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

.find(id) ⇒ Object



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

def self.find(id)
  obj = PROXY.get(resources_name, id: id).fetch('data', nil)
  return if obj.nil?

  to_instance(obj)
end

.idsObject



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

def self.ids
  get_all_ids
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.



72
73
74
75
76
77
78
79
80
# File 'lib/cropio/resource/base.rb', line 72

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)


56
57
58
# File 'lib/cropio/resource/base.rb', line 56

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

#saveObject

Saves current resource to Cropio.



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

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