sequel-tstzrange-fields 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2aa09554035af74a589ddd776eff11bf70fa33431c841bb20de3eef9674b4051
4
- data.tar.gz: 824c97bbb6ea48333980f88aa42f6b5b009f3b1d61fefb13646f54d6511eb378
3
+ metadata.gz: 602f1c9282af7052d0c4b5131a2f9872a383a51783f8f43bfd601066c5121136
4
+ data.tar.gz: 9d3594a668a50adb1ab8f88a33db59d4fc779d4012f281f01791f98ae74c302d
5
5
  SHA512:
6
- metadata.gz: c2f59a89cb58c004224b1931a3359bacc70c296c322d108f84082488fb225f1866168d3adfc2669cdbe45cb52819bfe42f3b9b8a9801ee3740ab4ec6a21264c6
7
- data.tar.gz: ec010f2605963f1cd27d03a1945854ed2f675b1fd77c9435f640673145cc636f64118ff24babd086689c2940a4656c799163d15c7c7df2466189784cb72076f9
6
+ metadata.gz: 327f56f8a6e62c01fd7c86620859a375fa3243f2019ce20e466f9e0651277803070618f4573f3272571c23b1defbee1a599826d2406507358e50e7c1d58c47b0
7
+ data.tar.gz: 18b71bc1c26e4913d4fb9f81f88ca2368c12f5bc9ff38ae483f8482566d67a9a080f7109abd4d8e36863897132a02c9cc5e7a28af09d6a690c20e205e0aa2a00
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sequel"
4
+ require "sequel/model"
5
+
6
+ require_relative "../../sequel_tstzrange_fields/version"
7
+
8
+ # Plugin for adding methods for working with time ranges.
9
+ #
10
+ # == Example
11
+ #
12
+ # Defining a model class with a timestamptz range:
13
+ #
14
+ # class ACME::Lease < Sequel::Model(:leases)
15
+ # plugin :tstzrange_fields, :active_during
16
+ #
17
+ # And in the schema:
18
+ #
19
+ # create_table(:leases) do
20
+ # primary_key :id
21
+ # tstzrange :active_during
22
+ # end
23
+ #
24
+ # You can use it as follows:
25
+ #
26
+ # lease = ACME::Lease.new
27
+ # lease.active_during_begin = Time.now
28
+ # lease.active_during_end = 1.year.from_now
29
+ # lease.active_during = 1.year.ago..1.year.from_now
30
+ # lease.active_during_end = nil # Unbounded end set
31
+ # lease.active_during = nil # Empty set
32
+ #
33
+ module Sequel
34
+ module Plugins
35
+ module TstzrangeFields
36
+ VERSION = SequelTstzrangeFields::VERSION
37
+
38
+ def self.configure(model, *args)
39
+ unless model.db.schema_type_class(:tstzrange)
40
+ msg = "tstzrange_fields plugin requires pg_range db extension to be installed. " \
41
+ "Use db.extension(:pg_range) after the db = Sequel.connect call."
42
+ raise msg
43
+ end
44
+ args << :period if args.empty?
45
+ args = args.flatten
46
+
47
+ setup_model(model)
48
+
49
+ args.flatten.each do |column|
50
+ create_accessors(model, column)
51
+ end
52
+ end
53
+
54
+ def self.setup_model(model)
55
+ model.class.define_method(:new_tstzrange) do |b, e|
56
+ b = value_to_time(b)
57
+ e = value_to_time(e)
58
+ return Sequel::Postgres::PGRange.empty(:tstzrange) if b.nil? && e.nil?
59
+
60
+ return Sequel::Postgres::PGRange.new(b&.to_time, e&.to_time, db_type: :tstzrange, exclude_end: true)
61
+ end
62
+
63
+ model.class.define_method(:value_to_time) do |v|
64
+ return v if v.nil?
65
+ return v if v.respond_to?(:to_time)
66
+
67
+ return Time.parse(v)
68
+ end
69
+ end
70
+
71
+ def self.create_accessors(model, column)
72
+ get_column_method = column.to_sym
73
+ set_column_method = "#{column}=".to_sym
74
+ get_begin_method = "#{column}_begin".to_sym
75
+ set_begin_method = "#{column}_begin=".to_sym
76
+ get_end_method = "#{column}_end".to_sym
77
+ set_end_method = "#{column}_end=".to_sym
78
+
79
+ model.define_method(get_column_method) do
80
+ self[column]
81
+ end
82
+
83
+ model.define_method(set_column_method) do |value|
84
+ case value
85
+ when Sequel::Postgres::PGRange
86
+ self[column] = value
87
+ when Float::INFINITY
88
+ range = Sequel::Postgres::PGRange.new(nil, nil, empty: false, db_type: :tstzrange)
89
+ self[column] = range
90
+ when "empty"
91
+ self[column] = Sequel::Postgres::PGRange.empty(:tstzrange)
92
+ else
93
+ beg = value.respond_to?(:begin) ? value.begin : (value[:begin] || value["begin"])
94
+ en = value.respond_to?(:end) ? value.end : (value[:end] || value["end"])
95
+ self[column] = self.class.new_tstzrange(beg, en)
96
+ end
97
+ end
98
+
99
+ model.define_method(get_begin_method) do
100
+ send(get_column_method).begin
101
+ end
102
+
103
+ model.define_method(set_begin_method) do |new_time|
104
+ new_range = self.class.new_tstzrange(new_time, send(get_end_method))
105
+ send(set_column_method, new_range)
106
+ end
107
+
108
+ model.define_method(get_end_method) do
109
+ r = send(get_column_method)
110
+ return r&.end
111
+ end
112
+
113
+ model.define_method(set_end_method) do |new_time|
114
+ new_range = self.class.new_tstzrange(send(get_begin_method), new_time)
115
+ send(set_column_method, new_range)
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SequelTstzrangeFields
4
+ VERSION = "0.2.1"
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-tstzrange-fields
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lithic Tech
@@ -94,14 +94,18 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description:
97
+ description: 'Gem for enabling time ranges when working with postgres
98
+
99
+ '
98
100
  email:
99
101
  - hello@lithic.tech
100
102
  executables: []
101
103
  extensions: []
102
104
  extra_rdoc_files: []
103
- files: []
104
- homepage:
105
+ files:
106
+ - lib/sequel/plugins/tstzrange_fields.rb
107
+ - lib/sequel_tstzrange_fields/version.rb
108
+ homepage: https://github.com/lithictech/sequel-tstzrange-fields
105
109
  licenses:
106
110
  - MIT
107
111
  metadata: