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 +4 -4
- data/README.md +54 -0
- data/lib/sequel/plugins/money_accessors.rb +66 -0
- data/lib/sequel/timestamp_migrator_undo_extension.rb +1 -1
- data/umbrellio-sequel-plugins.gemspec +2 -1
- data/utils/database.rb +1 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b042d58a702375bd77b7ca54edc5fcfda8bd60add56a949c7801969c3cc07d3f
|
4
|
+
data.tar.gz: e70ef852db7da472b4e31c918f96f8c81197449d1cf9f70e121c99bfb7ef9bfa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -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.
|
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
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.
|
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:
|
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
|
-
|
226
|
+
rubyforge_project:
|
227
|
+
rubygems_version: 2.7.7
|
212
228
|
signing_key:
|
213
229
|
specification_version: 4
|
214
230
|
summary: Sequel plugins
|