Class: ActiveMapper::Mapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapped_class, adapter) ⇒ Mapper

Returns a new instance of Mapper.



5
6
7
8
# File 'lib/active_mapper/mapper.rb', line 5

def initialize(mapped_class, adapter)
  @mapped_class = mapped_class
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



3
4
5
# File 'lib/active_mapper/mapper.rb', line 3

def adapter
  @adapter
end

#mapped_classObject (readonly)

Returns the value of attribute mapped_class.



3
4
5
# File 'lib/active_mapper/mapper.rb', line 3

def mapped_class
  @mapped_class
end

Instance Method Details

#all?(&block) ⇒ Boolean

Returns:



10
11
12
# File 'lib/active_mapper/mapper.rb', line 10

def all?(&block)
  count == count(&block)
end

#any?(&block) ⇒ Boolean

Returns:



14
15
16
# File 'lib/active_mapper/mapper.rb', line 14

def any?(&block)
  select(&block).any?
end

#avg(attribute) ⇒ Object Also known as: average



47
48
49
# File 'lib/active_mapper/mapper.rb', line 47

def avg(attribute)
  find_all.avg(attribute)
end

#clearObject Also known as: delete_all



102
103
104
# File 'lib/active_mapper/mapper.rb', line 102

def clear
  adapter.delete_all(mapped_class)
end

#count(&block) ⇒ Object



26
27
28
# File 'lib/active_mapper/mapper.rb', line 26

def count(&block)
  select(&block).count
end

#delete(object) ⇒ Object



90
91
92
# File 'lib/active_mapper/mapper.rb', line 90

def delete(object)
  adapter.delete(mapped_class, object)
end

#delete_if(&block) ⇒ Object



94
95
96
# File 'lib/active_mapper/mapper.rb', line 94

def delete_if(&block)
  adapter.delete_all(mapped_class, &block)
end

#find(id = nil, &block) ⇒ Object



56
57
58
# File 'lib/active_mapper/mapper.rb', line 56

def find(id = nil, &block)
  id ? first { |object| object.id == id } : first(&block)
end

#first(&block) ⇒ Object Also known as: detect



60
61
62
# File 'lib/active_mapper/mapper.rb', line 60

def first(&block)
  select(&block).first
end

#keep_if(&block) ⇒ Object



98
99
100
# File 'lib/active_mapper/mapper.rb', line 98

def keep_if(&block)
  delete_if { |object| !block.call(object) }
end

#last(&block) ⇒ Object



65
66
67
# File 'lib/active_mapper/mapper.rb', line 65

def last(&block)
  select(&block).last
end

#max(attribute) ⇒ Object Also known as: max_by, maximum



36
37
38
# File 'lib/active_mapper/mapper.rb', line 36

def max(attribute)
  find_all.max(attribute)
end

#min(attribute) ⇒ Object Also known as: min_by, minimum



30
31
32
# File 'lib/active_mapper/mapper.rb', line 30

def min(attribute)
  find_all.min(attribute)
end

#minmax(attribute) ⇒ Object Also known as: minmax_by



42
43
44
# File 'lib/active_mapper/mapper.rb', line 42

def minmax(attribute)
  find_all.minmax(attribute)
end

#none?(&block) ⇒ Boolean

Returns:



18
19
20
# File 'lib/active_mapper/mapper.rb', line 18

def none?(&block)
  select(&block).none?
end

#one?(&block) ⇒ Boolean

Returns:



22
23
24
# File 'lib/active_mapper/mapper.rb', line 22

def one?(&block)
  select(&block).one?
end

#reject(&block) ⇒ Object



74
75
76
# File 'lib/active_mapper/mapper.rb', line 74

def reject(&block)
  select { |object| !block.call(object) }
end

#save(object) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_mapper/mapper.rb', line 78

def save(object)
  return false unless object.valid?

  if object.id
    adapter.update(mapped_class, object)
  else
    object.id = adapter.insert(mapped_class, object)
  end

  object
end

#select(&block) ⇒ Object Also known as: find_all



69
70
71
# File 'lib/active_mapper/mapper.rb', line 69

def select(&block)
  Relation.new(mapped_class, adapter, &block)
end

#sum(attribute) ⇒ Object



52
53
54
# File 'lib/active_mapper/mapper.rb', line 52

def sum(attribute)
  find_all.sum(attribute)
end