Module: DataMapa::ClassMethods

Defined in:
lib/datamapa.rb

Instance Method Summary collapse

Instance Method Details

#active_record_class(klass) ⇒ Object

Declarative methods



13
14
15
# File 'lib/datamapa.rb', line 13

def active_record_class(klass)
  @ar_class = klass
end

#aggregates(components) ⇒ Object



33
34
35
# File 'lib/datamapa.rb', line 33

def aggregates(components)
  @aggregates = components
end

#composed_of(parts) ⇒ Object



37
38
39
# File 'lib/datamapa.rb', line 37

def composed_of(parts)
  @composed_of = parts
end

#composes(parent) ⇒ Object



41
42
43
# File 'lib/datamapa.rb', line 41

def composes(parent)
  @composes = parent
end

#create!(model, extras = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/datamapa.rb', line 72

def create!(model, extras={})
  begin
    attributes = attribute_hash(model, extras)

    ar = @ar_class.create!(attributes)
    model.send(:id=, ar.id)

    @composed_of.each do |parts, mapper|
      mapper.create_parts!(model.send(parts), model.id)
    end if @composed_of
    model
  rescue ActiveRecord::StatementInvalid => e
    raise DataMapa::PersistenceError, e.message
  end
end

#creates_model_with(&block) ⇒ Object



17
18
19
# File 'lib/datamapa.rb', line 17

def creates_model_with(&block)
  @create_model_proc = block
end

#delete!(id) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/datamapa.rb', line 111

def delete!(id)
  @composed_of.each do |part, mapper|
    mapper.delete_children_of(id)
  end if @composed_of

  count = @ar_class.delete(id)
end

#exists?(model) ⇒ Boolean

Returns:

  • (Boolean)


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

def exists?(model)
  if model.id.nil?
    load_id_with_semantic_key(model) unless @semantic_key.nil?
    !model.id.nil?
  else
    @ar_class.exists?(model.id)
  end
end

#find!(id) ⇒ Object

Public methods



47
48
49
50
51
52
53
54
# File 'lib/datamapa.rb', line 47

def find!(id)
  begin
    ar = @ar_class.find(id)
    model_for(ar)
  rescue ActiveRecord::RecordNotFound
    raise DataMapa::RecordNotFoundError
  end
end

#model_for(ar) ⇒ Object



119
120
121
122
123
124
# File 'lib/datamapa.rb', line 119

def model_for(ar)
  model = @create_model_proc.call(ar)
  r2o(ar, model)
  model.id = ar.id
  model
end

#ref_attr(attributes) ⇒ Object



29
30
31
# File 'lib/datamapa.rb', line 29

def ref_attr(attributes)
  @ref_attr = attributes
end

#save!(model, extras = {}) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/datamapa.rb', line 102

def save!(model, extras={})
  load_id_with_semantic_key(model) unless @semantic_key.nil?
  if model.id.nil?
    create!(model)
  else
    update(model)
  end
end

#semantic_key(key) ⇒ Object



21
22
23
# File 'lib/datamapa.rb', line 21

def semantic_key(key)
  @semantic_key = key
end

#simple_attr(attributes) ⇒ Object



25
26
27
# File 'lib/datamapa.rb', line 25

def simple_attr(attributes)
  @simple_attr = attributes
end

#update(model, extras = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/datamapa.rb', line 88

def update(model, extras={})
  begin
    attributes = attribute_hash(model, extras)

    @ar_class.update(model.id, attributes)

    @composed_of.each do |parts, mapper|
      mapper.update_parts!(model.send(parts), model.id)
    end if @composed_of
  rescue ActiveRecord::StatementInvalid => e
    raise DataMapa::PersistenceError, e.message
  end
end

#where(clause) ⇒ Object



65
66
67
68
69
70
# File 'lib/datamapa.rb', line 65

def where(clause)
  records = @ar_class.where(clause) 
  records.map do |ar|
    model_for(ar)
  end
end