Class: GcalMapper::Mapper::ActiveRecord::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/gcal_mapper/mapper/active_record.rb

Overview

Adapter for ActiveRecord

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



24
25
26
# File 'lib/gcal_mapper/mapper/active_record.rb', line 24

def initialize(base)
  @base = base
end

Instance Method Details

#create!(attributes) ⇒ Object

create and save object using attributes

return [Object] createrd object

Parameters:

  • attributes (Hash)

    all the attributes to save in the new object



32
33
34
# File 'lib/gcal_mapper/mapper/active_record.rb', line 32

def create!(attributes)
  @base.create(attributes)
end

#delete!(id) ⇒ Object

delet an object

Parameters:

  • id (Int)

    id of the object in the DB

Returns:

  • (Object)

    the destroyed object



50
51
52
53
# File 'lib/gcal_mapper/mapper/active_record.rb', line 50

def delete!(id)
  obj = @base.find(id)
  obj.destroy
end

#find_allArray

Find all models

Returns:

  • (Array)

    all the object from a calss



58
59
60
# File 'lib/gcal_mapper/mapper/active_record.rb', line 58

def find_all
  @base.all
end

#find_by(field, value) ⇒ Object

find an object from a field and a value

Parameters:

  • field (String)

    name of the field where to search

  • value (String)

    the value to find

Returns:

  • (Object)

    the finded object or nil



67
68
69
# File 'lib/gcal_mapper/mapper/active_record.rb', line 67

def find_by(field, value)
  @base.send('find_by_' + field, value)
end

#update!(id, attributes) ⇒ Object

update an object

Parameters:

  • id (Int)

    id of the object in the DB

  • attributes (Hash)

    hash containing the field to update

Returns:

  • (Object)

    updated object



41
42
43
44
# File 'lib/gcal_mapper/mapper/active_record.rb', line 41

def update!(id, attributes)
  obj = @base.find(id)
  obj.update_attributes!(attributes)
end