appydays 0.3.0 → 0.3.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: 6f235dd9ffc485d78888e9cc325dc090dba1f40b8cf4a2cb9e24df2074149520
4
- data.tar.gz: a34bed5f98cf79bad2401c94b254a41deb28f7165fb86e5378ec96bec8913b9d
3
+ metadata.gz: 03b299f65c7ee3543f139a2e705e1847144d49955dcab5fbe5c37a76fc9bb63a
4
+ data.tar.gz: 17179300f27c0f293a9ba7e1d106e87050c4ce1b73a99b69be572d51c07f982d
5
5
  SHA512:
6
- metadata.gz: 4e62429beede3533e76c7f245ca3b653c5b702f5fbd64571f7381a49c6975043e8d1c8a37f60a47c3aed7422ec83a8408da66852a47ca3c28840852b2e3feac0
7
- data.tar.gz: edb5ebdb0c6b073e8b6c8d4fdbd14d548592fbea901a6e6c0cfc7ef785231158ce94ff8bbbd5496637414c17239e47dd633e65db325c5316e8889d95d07fd564
6
+ metadata.gz: 35fa1b0bc6933d9b76d509e0321e36a1281983694aa0f0362d17420ec7cab8878daf9e4676312948e70db58bc976d2c166f5e7b6191b7f1b357fffb41f763ede
7
+ data.tar.gz: b1efd166a9f7d28bc3c3a1bb4bcaa6703cb4fa2a1ae983aa57675c100a786354975ed8ee38a67b8a27f1374c068dd2ec08176d46e525f900b99e94d3e089bf1f
@@ -25,6 +25,7 @@ module Appydays::SpecHelpers
25
25
 
26
26
  def matches?(target)
27
27
  @target = target
28
+ @target = @target.lines if @target.is_a?(String)
28
29
  return @target.find do |obj|
29
30
  obj.to_s.match(@regexp)
30
31
  end
@@ -43,9 +44,9 @@ module Appydays::SpecHelpers
43
44
  alias failure_message_for_should_not failure_message_when_negated
44
45
  end
45
46
 
46
- ### RSpec matcher -- set up the expectation that the lefthand side
47
- ### is Enumerable, and that at least one of the objects yielded
48
- ### while iterating matches +regexp+ when converted to a String.
47
+ # RSpec matcher -- set up the expectation that the lefthand side
48
+ # is Enumerable, and that at least one of the objects yielded
49
+ # while iterating matches +regexp+ when converted to a String.
49
50
  module_function def have_a_line_matching(regexp)
50
51
  return HaveALineMatching.new(regexp)
51
52
  end
@@ -54,39 +55,70 @@ module Appydays::SpecHelpers
54
55
  return RSpec::Matchers::BuiltIn::HaveAttributes.new(length: x)
55
56
  end
56
57
 
57
- # Matcher that will compare a string or time expected against a string or time actual,
58
- # within a tolerance (default to 1 millisecond).
59
- #
60
- # expect(last_response).to have_json_body.that_includes(
61
- # closes_at: match_time('2025-12-01T00:00:00.000+00:00').within(1.second))
62
- #
63
- RSpec::Matchers.define(:match_time) do |expected|
64
- match do |actual|
65
- @tolerance ||= 0.001
66
- RSpec::Matchers::BuiltIn::BeWithin.new(@tolerance).of(self.time(expected)).matches?(self.time(actual))
58
+ class MatchTime
59
+ def initialize(expected)
60
+ @expected = expected
61
+ if expected == :now
62
+ @expected_t = Time.now
63
+ @tolerance = 5
64
+ else
65
+ @expected_t = self.time(expected)
66
+ @tolerance = 0.001
67
+ end
67
68
  end
68
69
 
69
- failure_message do |actual|
70
- "expected ids %s to be within %s of %s" % [self.time(actual), @tolerance, self.time(expected)]
70
+ def time(s)
71
+ return nil if s.nil?
72
+ return Time.parse(s) if s.is_a?(String)
73
+ return s.to_time
74
+ end
75
+
76
+ def matches?(actual)
77
+ @actual_t = self.time(actual)
78
+ @actual_t = self.change_tz(@actual_t, @expected_t.zone) if @actual_t
79
+ return RSpec::Matchers::BuiltIn::BeWithin.new(@tolerance).of(@expected_t).matches?(@actual_t)
71
80
  end
72
81
 
73
- chain :within do |tolerance|
82
+ protected def change_tz(t, zone)
83
+ return t.change(zone: zone) if t.respond_to?(:change)
84
+ prev_tz = ENV.fetch("TZ", nil)
85
+ begin
86
+ ENV["TZ"] = zone
87
+ return Time.at(t.to_f)
88
+ ensure
89
+ ENV["TZ"] = prev_tz
90
+ end
91
+ end
92
+
93
+ def within(tolerance)
74
94
  @tolerance = tolerance
95
+ return self
75
96
  end
76
97
 
77
- def time(s)
78
- return Time.parse(s) if s.is_a?(String)
79
- return s.to_time
98
+ def failure_message
99
+ return "expected %s to be within %s of %s" % [@actual_t, @tolerance, @expected_t]
80
100
  end
81
101
  end
82
102
 
103
+ # Matcher that will compare a string or time expected against a string or time actual,
104
+ # within a tolerance (default to 1 millisecond).
105
+ #
106
+ # Use match_time(:now) to automatically `match_time(Time.now).within(5.seconds)`.
107
+ #
108
+ # expect(last_response).to have_json_body.that_includes(
109
+ # closes_at: match_time('2025-12-01T00:00:00.000+00:00').within(1.second))
110
+ #
111
+ def match_time(expected)
112
+ return MatchTime.new(expected)
113
+ end
114
+
83
115
  # Matcher that will compare a string or Money expected against a string or Money actual.
84
116
  #
85
117
  # expect(order.total).to cost('$25')
86
118
  #
87
- RSpec::Matchers.define(:cost) do |expected|
119
+ RSpec::Matchers.define(:cost) do |expected, currency|
88
120
  match do |actual|
89
- @base = RSpec::Matchers::BuiltIn::Eq.new(self.money(expected))
121
+ @base = RSpec::Matchers::BuiltIn::Eq.new(self.money(expected, currency))
90
122
  @base.matches?(self.money(actual))
91
123
  end
92
124
 
@@ -94,12 +126,37 @@ module Appydays::SpecHelpers
94
126
  @base.failure_message
95
127
  end
96
128
 
97
- def money(s)
98
- return Monetize.parse(s) if s.is_a?(String)
129
+ def money(s, currency=nil)
130
+ if (m = self.tryparse(s, currency))
131
+ return m
132
+ end
99
133
  return s if s.is_a?(Money)
100
- return Money.new(s) if s.is_a?(Integer)
134
+ return Money.new(s, currency) if s.is_a?(Integer)
101
135
  return Money.new(s[:cents], s[:currency]) if s.respond_to?(:key?) && s.key?(:cents) && s.key?(:currency)
102
136
  raise "#{s} type #{s.class.name} not convertable to Money (add support or use supported type)"
103
137
  end
138
+
139
+ def tryparse(s, currency)
140
+ # We need to capture some global settings while we parse, and set them back after.
141
+ orig_default = Money.instance_variable_get(:@default_currency)
142
+ orig_assume = Monetize.assume_from_symbol
143
+ return nil unless s.is_a?(String)
144
+ # See https://github.com/RubyMoney/monetize/issues/161
145
+ # To get around this, need to use a valid custom currency
146
+ begin
147
+ Money::Currency.new("APPYDAYS")
148
+ rescue Money::Currency::UnknownCurrency
149
+ Money::Currency.register(iso_code: "APPYDAYS", subunit_to_unit: 1)
150
+ end
151
+ Money.default_currency = currency || orig_default || "APPYDAYS"
152
+ Monetize.assume_from_symbol = true
153
+ m = Monetize.parse!(s)
154
+ return m unless m.currency == "APPYDAYS"
155
+ raise Money::Currency::UnknownCurrency,
156
+ "Could not parse currency from '#{s}'. It needs a symbol, or set default_currency."
157
+ ensure
158
+ Money.default_currency = orig_default
159
+ Monetize.assume_from_symbol = orig_assume
160
+ end
104
161
  end
105
162
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Appydays
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: appydays
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lithic Tech
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-29 00:00:00.000000000 Z
11
+ date: 2022-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: monetize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: money
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rack
43
71
  requirement: !ruby/object:Gem::Requirement