lib/spontaneous/data_mapper.rb in spontaneous-0.2.0.beta5 vs lib/spontaneous/data_mapper.rb in spontaneous-0.2.0.beta6

- old
+ new

@@ -61,10 +61,18 @@ :revision_table, :revision_table?, :revision_from_table, :revision_history_table, :revision_archive_table, :revision_history_dataset, :revision_archive_dataset, :quote_identifier + def prepare(type, name, *values, &block) + active_scope.prepare(type, prepared_statement_namespace(name), &block) + end + + def prepared_statement_namespace(name) + "#{name}_#{current_revision || "editable"}_#{visible_only?}".to_sym + end + def visible(visible_only = true, &block) scope(current_revision, visible_only, &block) end def visible!(visible_only = true, &block) @@ -85,10 +93,22 @@ def editable!(&block) revision!(nil, &block) end + def editable? + current_revision.nil? + end + + def with(dataset, &block) + with!(dataset, &block) + end + + def with!(dataset, &block) + scope!(nil, false, dataset, &block) + end + def scope(revision, visible, &block) if use_current_scope?(revision, visible) if block_given? yield else @@ -97,38 +117,38 @@ else scope!(revision, visible, &block) end end - def scope!(revision, visible, &block) + def scope!(revision, visible, dataset = nil, &block) if block_given? r, v, d = @keys.values_at(:revision, :visible, :active_scope) thread = Thread.current state = [thread[r], thread[v], thread[d]] begin thread[r] = to_revision(revision) thread[v] = visible - thread[d] = current_scope + thread[d] = configured_scope_or_dataset(dataset) yield ensure thread[r], thread[v], thread[d] = state end else scope_for(revision, visible) end end def active_scope - Thread.current[@keys[:active_scope]] || current_scope + Thread.current[@keys[:active_scope]] || configured_scope end - def cached_dataset? + def cached_scope? !Thread.current[@keys[:active_scope]].nil? end def use_current_scope?(revision, visible) - cached_dataset? && + cached_scope? && (current_revision == to_revision(revision)) && ((visible || false) == visible_only?) end def to_revision(r) @@ -151,15 +171,28 @@ @schema.uids[id_string] end private - def current_scope + def configured_scope_or_dataset(dataset = nil) + return configured_scope if dataset.nil? + scope_with(dataset) + end + + def configured_scope scope_for(current_revision, visible_only?) end def scope_for(revision, visibility) - Scope.new(revision, visibility, @table, @schema) + scope_with(ds(revision, visibility)) + end + + def scope_with(dataset) + Scope.new(dataset, @schema) + end + + def ds(revision, visibility) + @table.dataset(revision, visibility) end end end end