Class: GcalMapper::Mapper::Simple::Simple

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

Overview

Adapter to use without an orm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Simple

new object

Parameters:

  • klass (class)

    class that need an adapter



26
27
28
29
# File 'lib/gcal_mapper/mapper/simple.rb', line 26

def initialize(base)
  @base = base
  @events = []
end

Instance Attribute Details

#eventsObject (readonly)

array of the synchronized events



21
22
23
# File 'lib/gcal_mapper/mapper/simple.rb', line 21

def events
  @events
end

Instance Method Details

#create!(attributes) ⇒ Object

create and save object using attributes

Parameters:

  • attributes (Hash)

    all the attributes to save in the new object



34
35
36
37
38
39
40
41
42
# File 'lib/gcal_mapper/mapper/simple.rb', line 34

def create!(attributes)
  obj = @base.new
  obj.id = @events.count
  attributes.each do |key, value|
    obj.send(key+'=', value)
  end

  @events << obj
end

#delete!(id) ⇒ Object

delet an object

Parameters:

  • id (Int)

    id of the object in the DB



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gcal_mapper/mapper/simple.rb', line 60

def delete!(id)
  @events[id] = nil

  @events.each_with_index do |item, index|
    if item
      if @events[index + 1]
        item = @events[index + 1]
        item.id =- 1
      end
    end
  end

  @events.delete(nil)
end

#find_allObject

Find all models



77
78
79
# File 'lib/gcal_mapper/mapper/simple.rb', line 77

def find_all
  @events
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



85
86
87
88
89
90
91
92
# File 'lib/gcal_mapper/mapper/simple.rb', line 85

def find_by(field, value)
  find_event = nil
  @events.each do |event|
    find_event = event if event.send(field) == value
  end

  find_event
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



48
49
50
51
52
53
54
55
# File 'lib/gcal_mapper/mapper/simple.rb', line 48

def update!(id, attributes)
  obj = @events[id]
  attributes.each do |key, value|
    obj.send(key+'=', value)
  end

  @events << obj
end