Module: Sequel::Plugins::Temporal

Defined in:
lib/sequel/plugins/temporal.rb

Defined Under Namespace

Modules: ClassMethods, DatasetMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.at(time) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'lib/sequel/plugins/temporal.rb', line 4

def self.at(time)
  raise ArgumentError, "requires a block" unless block_given?
  key = :sequel_plugins_temporal_now
  previous, Thread.current[key] = Thread.current[key], time.to_time
  yield
  Thread.current[key] = previous
end

.configure(master, opts = {}) ⇒ Object

Raises:

  • (Error)


16
17
18
19
20
21
22
23
24
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
# File 'lib/sequel/plugins/temporal.rb', line 16

def self.configure(master, opts = {})
  version = opts[:version_class]
  raise Error, "please specify version class to use for temporal plugin" unless version
  required = [:master_id, :created_at, :expired_at]
  missing = required - version.columns
  raise Error, "temporal plugin requires the following missing column#{"s" if missing.size>1} on version class: #{missing.join(", ")}" unless missing.empty?
  master.instance_eval do
    @version_class = version
    base_alias = name ? underscore(demodulize(name)) : table_name
    @versions_alias = "#{base_alias}_versions".to_sym
    @current_version_alias = "#{base_alias}_current_version".to_sym
  end
  master.one_to_many :versions, class: version, key: :master_id, graph_alias_base: master.versions_alias
  master.one_to_one :current_version, class: version, key: :master_id, graph_alias_base: master.current_version_alias, :graph_block=>(proc do |j, lj, js|
    n = ::Sequel::Plugins::Temporal.now
    e = :expired_at.qualify(j)
    (:created_at.qualify(j) <= n) & ({e=>nil} | (e > n))
  end) do |ds|
    n = ::Sequel::Plugins::Temporal.now
    ds.where{(created_at <= n) & ({expired_at=>nil} | (expired_at > n))}
  end
  master.def_dataset_method :with_current_version do
    eager_graph(:current_version).where({:id.qualify(model.current_version_alias) => nil}.sql_negate)
  end
  version.many_to_one :master, class: master, key: :master_id
  version.class_eval do
    def current?
      n = ::Sequel::Plugins::Temporal.now
      !new? &&
      created_at.to_time<=n &&
      (expired_at.nil? || expired_at.to_time>n)
    end
  end
  unless opts[:delegate]==false
    (version.columns-required-[:id]).each do |column|
      master.class_eval <<-EOS
        def #{column}
          pending_or_current_version.#{column} if pending_or_current_version
        end
      EOS
    end
  end
end

.nowObject



12
13
14
# File 'lib/sequel/plugins/temporal.rb', line 12

def self.now
  Thread.current[:sequel_plugins_temporal_now] || Time.now
end