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:

  • (Boolean)


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

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

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


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



106
107
108
# File 'lib/active_mapper/mapper.rb', line 106

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



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

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

#delete_if(&block) ⇒ Object



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

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

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



56
57
58
59
60
61
62
# File 'lib/active_mapper/mapper.rb', line 56

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

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



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

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

#keep_if(&block) ⇒ Object



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

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

#last(&block) ⇒ Object



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

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:

  • (Boolean)


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

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

#one?(&block) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#reject(&block) ⇒ Object



78
79
80
# File 'lib/active_mapper/mapper.rb', line 78

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

#save(object) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_mapper/mapper.rb', line 82

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



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

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