umbrellio-sequel-plugins 0.4.0.81 → 0.4.0.107

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: 1506132f68e964f7e3f06370a9766cdad8b89d84ad4e810e73ab3225e6f1140b
4
- data.tar.gz: 0f1774262d2a637abd9dfd8a5bd379a2a22cac5a0f0836c7c742e115657ff467
3
+ metadata.gz: b042d58a702375bd77b7ca54edc5fcfda8bd60add56a949c7801969c3cc07d3f
4
+ data.tar.gz: e70ef852db7da472b4e31c918f96f8c81197449d1cf9f70e121c99bfb7ef9bfa
5
5
  SHA512:
6
- metadata.gz: 3c2f62536da956885da8d21c21ca9704925e85dce9de4eafd24948e30dea6b354281384529b80f7bd6f1076f17348bb58e2aff378ec6d8a54b285e367f26b7cb
7
- data.tar.gz: 58e5e45610d3dd9746b17bef038fd4609634d1050ed5619766dd30eeecf8c1fa5dc64b710c0b1cfa2a2b0660786a01e3264c6f21334a935bbce0d4aef7415ff7
6
+ metadata.gz: b9013cb8c53474751b0646af8d4fef024dc7665f87b9926dd68d89c7c92adaa9b207d290578e9cff4a3de0bbf3ca19b55d01bc808e1f6627816bcc48ff10a459
7
+ data.tar.gz: 87e04be02551911c2eb512a4fafe812e99add5091d087ccdbbd82dcd23b9a872e6e277c10bbdf2276d006fd076f26b7d562d81ca78a3ecfed59a7b2df4e35b40
data/README.md CHANGED
@@ -237,6 +237,60 @@ item.price # => #<Money fractional:5000.0 currency:USD>
237
237
  item.get_column_value(:amount) # => 0.5e2
238
238
  ```
239
239
 
240
+ ## MoneyAccessors
241
+
242
+ **Important:** requires `money` gem described above.
243
+
244
+ Plugin for using money field keys as model properties.
245
+
246
+ Enable:
247
+
248
+ ```ruby
249
+ add_runtime_dependency "money"
250
+ Sequel::Model.plugin :money_accessors
251
+ ````
252
+
253
+ Examples of usage:
254
+
255
+ ##### Money accessor
256
+
257
+ ```ruby
258
+ class Order < Sequel::Model
259
+ money_accessor :amount, :currency
260
+ end
261
+
262
+ order = Order.create(amount: 200, currency: "EUR")
263
+ order.amount # => #<Money fractional:20000.0 currency:EUR>
264
+ order.currency # => "EUR"
265
+
266
+ order.amount = Money.new(150, "RUB")
267
+ order.amount # => #<Money fractional:150.0 currency:RUB>
268
+ ```
269
+
270
+ ##### Money setter
271
+
272
+ ```ruby
273
+ class Order < Sequel::Model
274
+ money_setter :amount, :currency
275
+ end
276
+
277
+ order = Order.create(amount: 200, currency: "EUR")
278
+ order.amount = Money.new(150, "RUB")
279
+ order.currency # => "RUB"
280
+ ```
281
+
282
+ ##### Money getter
283
+
284
+ ```ruby
285
+ class Order < Sequel::Model
286
+ money_getter :amount, :currency
287
+ end
288
+
289
+ order = Order.create(amount: 200, currency: "EUR")
290
+ order.amount # => #<Money fractional:20000.0 currency:EUR>
291
+ order.currency # => "EUR"
292
+ ```
293
+
240
294
  ## StoreAccessors
241
295
 
242
296
  Enable: `Sequel::Model.plugin :store_accessors`
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "money"
4
+
5
+ # Creates accessors for money values
6
+ module Sequel::Plugins::MoneyAccessors
7
+ MoneyClassRequired = Class.new(StandardError)
8
+
9
+ module ClassMethods
10
+ # Setup money accessor
11
+ #
12
+ # @param amount_column [Symbol] amount column
13
+ # @param currency_column [Symbol] currency column
14
+ # @example
15
+ # class Order < Sequel::Model
16
+ # money_accessor :amount, :currency
17
+ # end
18
+ #
19
+ # order = Order.create(amount: 200, currency: "EUR")
20
+ # order.amount # => #<Money fractional:20000.0 currency:RUB>
21
+ # order.currency # => "EUR"
22
+ def money_accessor(amount_column, currency_column)
23
+ money_getter(amount_column, currency_column)
24
+ money_setter(amount_column, currency_column)
25
+ end
26
+
27
+ def money_getter(amount_column, currency_column)
28
+ include_accessors_module!
29
+ @_money_accessors_module.module_eval do
30
+ define_method(amount_column) do
31
+ amount, currency = super(), send(currency_column)
32
+ return unless amount && currency
33
+ Money.from_amount(amount.to_d, currency)
34
+ end
35
+ end
36
+ end
37
+
38
+ def money_setter(amount_column, currency_column)
39
+ include_accessors_module!
40
+ @_money_accessors_module.module_eval do
41
+ define_method("#{amount_column}=") do |value|
42
+ case value
43
+ when Money
44
+ amount = value.to_d
45
+ currency = value.currency.to_s
46
+ when nil
47
+ amount = currency = nil
48
+ else
49
+ raise MoneyClassRequired, "#{amount_column} value must be either Money instance or nil"
50
+ end
51
+
52
+ super(amount)
53
+ send("#{currency_column}=", currency)
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def include_accessors_module!
61
+ return if defined?(@_money_accessors_module)
62
+ @_money_accessors_module = Module.new
63
+ prepend @_money_accessors_module
64
+ end
65
+ end
66
+ end
@@ -23,7 +23,7 @@ module Sequel
23
23
  ds.filter(column => filename).delete
24
24
  end
25
25
 
26
- elapsed = format("%0.6f", Time.now - time)
26
+ elapsed = format("%<time>0.6f", time: Time.now - time)
27
27
  db.log_info("Finished undoing migration #{filename}, took #{elapsed} seconds")
28
28
  end
29
29
 
@@ -21,11 +21,12 @@ Gem::Specification.new do |spec|
21
21
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_dependency "sequel"
24
+ spec.add_runtime_dependency "sequel"
25
25
  spec.add_runtime_dependency "symbiont-ruby", ">= 0.6"
26
26
 
27
27
  spec.add_development_dependency "bundler"
28
28
  spec.add_development_dependency "coveralls"
29
+ spec.add_development_dependency "money"
29
30
  spec.add_development_dependency "pg"
30
31
  spec.add_development_dependency "pry"
31
32
  spec.add_development_dependency "rake"
data/utils/database.rb CHANGED
@@ -24,6 +24,7 @@ Sequel.extension :pg_range_ops
24
24
 
25
25
  Sequel::Model.plugin :duplicate
26
26
  Sequel::Model.plugin :get_column_value
27
+ Sequel::Model.plugin :money_accessors
27
28
  Sequel::Model.plugin :store_accessors
28
29
  Sequel::Model.plugin :synchronize
29
30
  Sequel::Model.plugin :upsert
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umbrellio-sequel-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.81
4
+ version: 0.4.0.107
5
5
  platform: ruby
6
6
  authors:
7
7
  - nulldef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-19 00:00:00.000000000 Z
11
+ date: 2020-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: money
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: pg
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -177,6 +191,7 @@ files:
177
191
  - lib/sequel/extensions/synchronize.rb
178
192
  - lib/sequel/plugins/duplicate.rb
179
193
  - lib/sequel/plugins/get_column_value.rb
194
+ - lib/sequel/plugins/money_accessors.rb
180
195
  - lib/sequel/plugins/store_accessors.rb
181
196
  - lib/sequel/plugins/synchronize.rb
182
197
  - lib/sequel/plugins/upsert.rb
@@ -208,7 +223,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
223
  - !ruby/object:Gem::Version
209
224
  version: '0'
210
225
  requirements: []
211
- rubygems_version: 3.0.3
226
+ rubyforge_project:
227
+ rubygems_version: 2.7.7
212
228
  signing_key:
213
229
  specification_version: 4
214
230
  summary: Sequel plugins