shopify-money 0.15.0 → 1.0.1.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
- module MoneyAccessor
3
- def self.included(base)
4
- base.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def money_accessor(*columns)
9
- variable_get = self <= Struct ? :[] : :instance_variable_get
10
- variable_set = self <= Struct ? :[]= : :instance_variable_set
11
-
12
- Array(columns).flatten.each do |name|
13
- variable_name = self <= Struct ? name : "@#{name}"
14
-
15
- define_method(name) do
16
- value = public_send(variable_get, variable_name)
17
- value.blank? ? nil : Money.new(value)
18
- end
19
-
20
- define_method("#{name}=") do |value|
21
- if value.blank? || !value.respond_to?(:to_money)
22
- public_send(variable_set, variable_name, nil)
23
- nil
24
- else
25
- money = value.to_money
26
- public_send(variable_set, variable_name, money.value)
27
- money
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'spec_helper'
3
-
4
- class NormalObject
5
- include MoneyAccessor
6
-
7
- money_accessor :price
8
-
9
- def initialize(price)
10
- @price = price
11
- end
12
- end
13
-
14
- class StructObject < Struct.new(:price)
15
- include MoneyAccessor
16
-
17
- money_accessor :price
18
- end
19
-
20
- RSpec.shared_examples_for "an object with a money accessor" do
21
- it "generates an attribute reader that returns a money object" do
22
- object = described_class.new(100)
23
-
24
- expect(object.price).to eq(Money.new(100))
25
- end
26
-
27
- it "generates an attribute reader that returns a nil object if the value was nil" do
28
- object = described_class.new(nil)
29
-
30
- expect(object.price).to eq(nil)
31
- end
32
-
33
- it "generates an attribute reader that returns a nil object if the value was blank" do
34
- object = described_class.new('')
35
-
36
- expect(object.price).to eq(nil)
37
- end
38
-
39
- it "generates an attribute writer that allow setting a money object" do
40
- object = described_class.new(0)
41
- object.price = Money.new(10)
42
-
43
- expect(object.price).to eq(Money.new(10))
44
- end
45
-
46
- it "generates an attribute writer that allow setting a integer value" do
47
- object = described_class.new(0)
48
- object.price = 10
49
-
50
- expect(object.price).to eq(Money.new(10))
51
- end
52
-
53
- it "generates an attribute writer that allow setting a float value" do
54
- object = described_class.new(0)
55
- object.price = 10.12
56
-
57
- expect(object.price).to eq(Money.new(10.12))
58
- end
59
-
60
- it "generates an attribute writer that allow setting a nil value" do
61
- object = described_class.new(0)
62
- object.price = nil
63
-
64
- expect(object.price).to eq(nil)
65
- end
66
-
67
- it "generates an attribute writer that allow setting a blank value" do
68
- object = described_class.new(0)
69
- object.price = ''
70
-
71
- expect(object.price).to eq(nil)
72
- end
73
- end
74
-
75
- RSpec.describe NormalObject do
76
- it_behaves_like "an object with a money accessor"
77
- end
78
-
79
- RSpec.describe StructObject do
80
- it_behaves_like "an object with a money accessor"
81
-
82
- it 'does not generate an ivar to store the price value' do
83
- object = described_class.new(10.00)
84
-
85
- expect(object.instance_variable_get(:@price)).to eq(nil)
86
- end
87
- end