Module: Wepo::Repo

Defined in:
lib/wepo/repo.rb

Overview

Repo base class

Author:

  • Josh Deeden <jdeeden@gmail.com>

Since:

  • 0.1.0

Version:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject (readonly)

Since:

  • 0.1.0


12
13
14
# File 'lib/wepo/repo.rb', line 12

def adapter
  @adapter
end

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wepo/repo.rb', line 18

def self.included(base)
  base.instance_eval do
    extend Uber::InheritableAttr
    inheritable_attr :model_class
    inheritable_attr :entity_class

    self.model_class = nil
    self.entity_class = nil

    def model(*args)
      if args[0]
        self.model_class = args[0]
      else
        self.model_class
      end
    end

    def entity(*args)
      if args[0]
        self.entity_class = args[0]
      else
        self.entity_class
      end
    end
  end
end

Instance Method Details

#allObject

Since:

  • 0.1.0


45
46
47
# File 'lib/wepo/repo.rb', line 45

def all
  adapter.all(model_class).map(&method(:map_model))
end

#create(entity) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.1.0


65
66
67
68
# File 'lib/wepo/repo.rb', line 65

def create(entity)
  raise ArgumentError, 'entity cannot be nil' if entity.nil?
  map_model adapter.create(map_entity(entity))
end

#delete(entity) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.1.0


75
76
77
78
# File 'lib/wepo/repo.rb', line 75

def delete(entity)
  raise ArgumentError, 'entity cannot be nil' if entity.nil?
   map_model adapter.delete(map_entity(entity))
end

#find(id) ⇒ Object

Since:

  • 0.1.0


49
50
51
52
53
54
55
# File 'lib/wepo/repo.rb', line 49

def find(id)
  begin
    adapter.find(model_class, id)
  rescue
    raise EntityNotFound, "Could not find #{model_class} with id: #{id}"
  end
end

#find_or_initialize_by(params) ⇒ Object

Since:

  • 0.1.0


57
58
59
# File 'lib/wepo/repo.rb', line 57

def find_or_initialize_by(params)
  map_model adapter.find_or_initialize_by(model_class, params)
end

#initialize(adapter) ⇒ Object

Since:

  • 0.1.0


14
15
16
# File 'lib/wepo/repo.rb', line 14

def initialize(adapter)
  @adapter = adapter
end

#save(entity) ⇒ Object

Since:

  • 0.1.0


80
81
82
# File 'lib/wepo/repo.rb', line 80

def save(entity)
  entity.id.nil? ? create(entity) : update(entity)
end

#update(entity) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.1.0


70
71
72
73
# File 'lib/wepo/repo.rb', line 70

def update(entity)
  raise ArgumentError, 'entity cannot be nil' if entity.nil?
  map_model adapter.update(map_entity(entity))
end

#where(params) ⇒ Object

Since:

  • 0.1.0


61
62
63
# File 'lib/wepo/repo.rb', line 61

def where(params)
  adapter.where(model_class, params).map(&method(:map_model))
end