Class: M4DBI::Collection
Instance Method Summary
collapse
Constructor Details
#initialize(the_one, the_many_model, the_one_fk) ⇒ Collection
3
4
5
6
7
|
# File 'lib/m4dbi/collection.rb', line 3
def initialize( the_one, the_many_model, the_one_fk )
@the_one = the_one
@the_many_model = the_many_model
@the_one_fk = the_one_fk
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
14
15
16
|
# File 'lib/m4dbi/collection.rb', line 14
def method_missing( method, *args, &blk )
elements.send( method, *args, &blk )
end
|
Instance Method Details
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/m4dbi/collection.rb', line 66
def clear
stm = @the_many_model.dbh.prepare(
%{
DELETE FROM #{@the_many_model.table}
WHERE #{@the_one_fk} = ?
}
)
num_deleted = stm.execute( @the_one.pk ).affected_count
stm.finish
num_deleted
end
|
#delete(arg) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/m4dbi/collection.rb', line 25
def delete( arg )
case arg
when @the_many_model
stm = @the_many_model.dbh.prepare(
%{
DELETE FROM #{@the_many_model.table}
WHERE
#{@the_one_fk} = ?
AND #{@the_many_model.pk_clause}
}
)
success = stm.execute(
@the_one.pk,
arg.pk
).affected_count > 0
stm.finish
success
when Hash
hash = arg
keys = hash.keys
where_subclause = keys.map { |k|
"#{k} = ?"
}.join( " AND " )
stm = @the_many_model.dbh.prepare(
%{
DELETE FROM #{@the_many_model.table}
WHERE
#{@the_one_fk} = ?
AND #{where_subclause}
}
)
num_deleted = stm.execute(
@the_one.pk,
*( keys.map { |k| hash[ k ] } )
).affected_count
stm.finish
num_deleted
end
end
|
#elements ⇒ Object
Also known as:
copy
9
10
11
|
# File 'lib/m4dbi/collection.rb', line 9
def elements
@the_many_model.where( @the_one_fk => @the_one.pk )
end
|
#push(new_item_hash) ⇒ Object
Also known as:
<<, add
18
19
20
21
|
# File 'lib/m4dbi/collection.rb', line 18
def push( new_item_hash )
new_item_hash[ @the_one_fk ] = @the_one.pk
@the_many_model.create( new_item_hash )
end
|