class Spigot::Record

Attributes

data[R]
Record

Spigot::Record is responsible for the instantiation and creation of objects with the formatted data received from Spigot::Translator.

map[R]
Record

Spigot::Record is responsible for the instantiation and creation of objects with the formatted data received from Spigot::Translator.

record[R]
Record

Spigot::Record is responsible for the instantiation and creation of objects with the formatted data received from Spigot::Translator.

resource[R]
Record

Spigot::Record is responsible for the instantiation and creation of objects with the formatted data received from Spigot::Translator.

service[R]
Record

Spigot::Record is responsible for the instantiation and creation of objects with the formatted data received from Spigot::Translator.

Public Class Methods

create(service, resource, data) click to toggle source
#create(resource, data)

Executes the create method on the implementing resource with formatted data.

@param resource [Object] This is the class implementing the record. @param data [Hash] The already formatted data used to produce the object.

# File lib/spigot/record.rb, line 41
def self.create(service, resource, data)
  new(service, resource, data).create
end
instantiate(service, resource, data) click to toggle source
#instantiate(resource, data)

Executes the initialize method on the implementing resource with formatted data.

@param resource [Object] This is the class implementing the record. @param data [Hash] The already formatted data used to produce the object.

# File lib/spigot/record.rb, line 32
def self.instantiate(service, resource, data)
  new(service, resource, data).instantiate
end
new(service, resource, data, record = nil) click to toggle source

initialize(resource, data) Method to initialize a record.

@param resource [Object] This is the class implementing the record. @param data [Hash] The already formatted data used to produce the object. @param record [Object] Optional record of `resource` type already in database.

# File lib/spigot/record.rb, line 16
def initialize(service, resource, data, record = nil)
  @resource     = resource
  @data         = data
  @record       = record
  @service      = service

  proxy = resource.spigot(service)
  @map          = proxy.map if proxy.present?
  @associations = map ? map.associations : []
end
update(service, resource, record, data) click to toggle source
#update(resource, data)

Assigns the formatted data to the resource and saves.

@param resource [Object] This is the class implementing the record. @param record [Object] Optional record of `resource` type already in database. @param data [Hash] The already formatted data used to produce the object.

# File lib/spigot/record.rb, line 51
def self.update(service, resource, record, data)
  new(service, resource, data, record).update
end

Public Instance Methods

create() click to toggle source
#create

Executes the create method on the implementing resource with formatted data.

# File lib/spigot/record.rb, line 63
def create
  data.is_a?(Array) ? create_by_array : create_by_hash(data)
end
instantiate() click to toggle source
#instantiate

Executes the initialize method on the implementing resource with formatted data.

# File lib/spigot/record.rb, line 57
def instantiate
  resource.new(data)
end
update() click to toggle source
#update

Assigns the formatted data to the resource and saves.

# File lib/spigot/record.rb, line 69
def update
  record.assign_attributes(data)
  record.save! if record.changed?
end

Private Instance Methods

create_by_array() click to toggle source
# File lib/spigot/record.rb, line 76
def create_by_array
  data.map { |record| create_by_hash(record) }
end
create_by_hash(record) click to toggle source
# File lib/spigot/record.rb, line 80
def create_by_hash(record)
  resolve_associations(record) if @associations.any?
  resource.create(record)
end
resolve_associations(record) click to toggle source
# File lib/spigot/record.rb, line 85
def resolve_associations(record)
  @associations.each do |association|
    submodel = association.instance_variable_get(:@value)

    key = submodel.name.underscore.to_sym
    submodel_data = record.delete(key)
    if submodel_data
      object = Record.create(service, submodel, submodel_data)
      record.merge!("#{key}_id".to_sym => object.id)
    end
  end
end