code-ruby 0.8.2 → 0.8.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70c443c88335cbe52c883ee9443aa32649b0fc3d294864fc69118bc2aa56688a
4
- data.tar.gz: 25fcebbe1193d7f75c820f550400187f6082ac0e58346291d0fc53114a497a09
3
+ metadata.gz: f5dda57442d7fc50b08ceb9998021e623f226a0b8de3cdaf58fecd05d0ba3a30
4
+ data.tar.gz: bead8ebd992882ea955f10d24e10f110a5c9955926e0e4850cddbcb4e3afe34c
5
5
  SHA512:
6
- metadata.gz: 0f558cf8b686f6457a83fcd383243810e6a2d611d7e1dd039991fce5947c740df19d445d3ae0dcefc4973c7282e1e201cd380b3ee5f5ec67588d257ac4b61db7
7
- data.tar.gz: acea5e97f3b15e08392331ef776827fe3a115a42603bdc958c0dd348835fb5330ac333626ca0337f22d5b5d45d50f615ef71d66132b9876b04efa9a2abda607c
6
+ metadata.gz: 136c900a7f0efab8fbf2ada6bc90c018689f5545b350b1b3b77c19e07ebd2eb18addb6852a12ede8c7bb692ad48af341c9decab8740b26d92fd5f45ec2a5ffa6
7
+ data.tar.gz: 24881df454cb5e179265232a37c45a1d1322ed0606ed3caf82fc56ee3526df1b3dc3c1f06301f5887e48b5da65e2b01b9c4e8424c703ab47923325c3b5ee1d6f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (0.8.1)
4
+ code-ruby (0.8.4)
5
5
  activesupport (~> 7)
6
6
  bigdecimal (~> 3)
7
7
  language-ruby (~> 0)
@@ -17,6 +17,9 @@ class Code
17
17
  operator = args.fetch(:operator, nil)
18
18
 
19
19
  case operator.to_s
20
+ when "ago"
21
+ sig(args)
22
+ code_ago
20
23
  when "from_now"
21
24
  sig(args)
22
25
  code_from_now
@@ -25,6 +28,10 @@ class Code
25
28
  end
26
29
  end
27
30
 
31
+ def code_ago
32
+ Time.new(raw.ago)
33
+ end
34
+
28
35
  def code_from_now
29
36
  Time.new(raw.from_now)
30
37
  end
@@ -82,6 +82,9 @@ class Code
82
82
  when "clone"
83
83
  sig(args)
84
84
  code_clone
85
+ when "day", "days"
86
+ sig(args)
87
+ code_days
85
88
  when "decrement!"
86
89
  sig(args) { Integer.maybe }
87
90
  code_decrement!(value)
@@ -389,6 +392,10 @@ class Code
389
392
  Duration.new(raw.hours)
390
393
  end
391
394
 
395
+ def code_days
396
+ Duration.new(raw.days)
397
+ end
398
+
392
399
  def inspect
393
400
  to_s
394
401
  end
@@ -19,6 +19,9 @@ class Code
19
19
  operator = args.fetch(:operator, nil)
20
20
 
21
21
  case operator.to_s
22
+ when "now"
23
+ sig(args)
24
+ code_now
22
25
  when "tomorrow"
23
26
  sig(args)
24
27
  code_tomorrow
@@ -32,6 +35,38 @@ class Code
32
35
  new(::Time.zone.tomorrow.beginning_of_day)
33
36
  end
34
37
 
38
+ def self.code_now
39
+ ::Time.zone ||= DEFAULT_ZONE
40
+ new(::Time.zone.now.beginning_of_day)
41
+ end
42
+
43
+ def call(**args)
44
+ operator = args.fetch(:operator, nil)
45
+ arguments = args.fetch(:arguments, [])
46
+ value = arguments.first&.value
47
+
48
+ case operator.to_s
49
+ when "after?"
50
+ sig(args) { Time.maybe }
51
+ code_after?(value)
52
+ when "before?"
53
+ sig(args) { Time.maybe }
54
+ code_before?(value)
55
+ else
56
+ super
57
+ end
58
+ end
59
+
60
+ def code_after?(other)
61
+ other ||= Time.code_now
62
+ Boolean.new(raw.after?(other.raw))
63
+ end
64
+
65
+ def code_before?(other)
66
+ other ||= Time.code_now
67
+ Boolean.new(raw.before?(other.raw))
68
+ end
69
+
35
70
  def inspect
36
71
  to_s
37
72
  end
data/lib/code/object.rb CHANGED
@@ -146,7 +146,7 @@ class Code
146
146
  end
147
147
 
148
148
  def self.name
149
- raise NotImplementedError, "name"
149
+ "Object"
150
150
  end
151
151
 
152
152
  def self.to_s
data/lib/code/version.rb CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require_relative "../code"
4
4
 
5
- Code::Version = Gem::Version.new("0.8.2")
5
+ Code::Version = Gem::Version.new("0.8.4")
data/spec/code_spec.rb CHANGED
@@ -4,17 +4,6 @@ require "spec_helper"
4
4
 
5
5
  RSpec.describe Code do
6
6
  [
7
- %w[9975×14÷8 17456.25],
8
- ["\r\n", "nothing"],
9
- ["1 + 1", "2"],
10
- ["a = 1", "1"],
11
- ["a = 1 b = 2 c = a + b c", "3"],
12
- ["a = 1 a += 1 a", "2"],
13
- ["a = b = c = 1 a + b + c", "3"],
14
- ["3.times {}", "3"],
15
- ["a = 1 3.times { a += 1 } a", "4"],
16
- ['user = {} user[:name] = "Dorian" user[:name]', ":Dorian"],
17
- ['user = {} user.name = "Dorian" user.name', ":Dorian"],
18
7
  [
19
8
  "user = { name: :Dorian, age: 31 } user.delete_if(String) user.keys",
20
9
  '["age"]'
@@ -23,116 +12,136 @@ RSpec.describe Code do
23
12
  "(user = { name: :Dorian, age: 31 }).transform_values { user.name }.age",
24
13
  ":Dorian"
25
14
  ],
15
+ %w[!!1 true],
16
+ %w[!!true true],
17
+ %w[!false true],
18
+ %w["" ''],
19
+ %w["Hello".downcase :hello],
20
+ %w['' ""],
21
+ %w[+1 1],
22
+ %w[+1.0 1.0],
23
+ %w[+true true],
24
+ %w[--1 1],
25
+ %w[--1.0 1.0],
26
+ %w[-1 -1],
27
+ %w[-1.0 -1.0],
28
+ %w[0 0],
29
+ %w[0b10 2],
30
+ %w[0o10 8],
31
+ %w[0x10 16],
32
+ %w[1.0e1 10.0],
33
+ %w[1.1 1.1],
34
+ %w[10e1.0 100.0],
35
+ %w[1e1 10],
36
+ %w[9975×14÷8 17456.25],
37
+ %w[:Hello.include?(:H) true],
38
+ %w[:admin? :admin?],
39
+ %w[:hello :hello],
40
+ %w[:update!.to_string :update!],
41
+ %w[false false],
42
+ %w[true true],
43
+ %w[{} {}],
44
+ ["'Hello Dorian'", '"Hello Dorian"'],
45
+ ["'Hello \\{name}'", ':Hello + " \\{name}"'],
46
+ ["'Hello {1}'", '"Hello 1"'],
47
+ ["(true false)", "false"],
26
48
  ["1 & 1", "1"],
27
- ["true && false", "false"],
28
- ["true && true", "true"],
29
- ["1 | 2", "3"],
49
+ ["1 + 1", "2"],
50
+ ["1 < 2", "true"],
51
+ ["1 << 1", "2"],
52
+ ["1 <= 1", "true"],
53
+ ["1 == 1 and 2 == 2", "true"],
54
+ ["1 == 1", "true"],
55
+ ["1 > 2", "false"],
56
+ ["1 >= 1", "true"],
57
+ ["1 >> 1", "0"],
30
58
  ["1 ^ 2", "3"],
31
- %w[true true],
32
- %w[false false],
59
+ ["1 | 2", "3"],
60
+ ["1.day.ago.after?(1.day.from_now)", 'false'],
61
+ ["1.day.ago.before?(1.day.from_now)", 'true'],
62
+ ["1.day.from_now.after?(1.day.ago)", 'true'],
63
+ ["1.day.from_now.before?(1.day.ago)", 'false'],
64
+ ["1.day.ago.after?", 'false'],
65
+ ["1.day.ago.before?", 'true'],
66
+ ["1.day.from_now.after?", "true"],
67
+ ["1.day.from_now.before?", "false"],
68
+ ["2 * 3 + 2", "8"],
69
+ ["2 * 3", "6"],
70
+ ["2 ** 3 ** 2", "512"],
71
+ ["2 ** 3", "8"],
72
+ ["2 + 2 * 3", "8"],
73
+ ["2 / 4", "0.5"],
74
+ ["3.times {}", "3"],
75
+ ["4 % 3", "1"],
76
+ ["[,,].include?(4)", "false"],
77
+ ["[,].include?(4)", "false"],
78
+ ["[1, 2, 3, \n ].include?(4)", "false"],
79
+ ["[1, 2, 3,].include?(4)", "false"],
80
+ ["[1, 2, 3]", "[1, 2, 3]"],
81
+ ["[1, 2, 3].include?(2)", "true"],
82
+ ["[1, 2, 3].include?(4)", "false"],
33
83
  ["[1, 2, 3].select { |n| n.even? }", "[2]"],
34
84
  ["[1, 2, 3].select { |n| n.even? }.select { |n| n.odd? }", "[]"],
35
- %w[{} {}],
36
- ["{ a: 1, b: 2, c: 3 }", '{"a" => 1, "b" => 2, "c" => 3}'],
37
- ['{ "first_name": "Dorian" }', '{"first_name" => "Dorian"}'],
38
- ["a = 1 a", "1"],
85
+ ["[1, 2].map(&:to_string)", "[:1, :2]"],
86
+ ["[1, 2].map(&:to_string)", '["1", "2"]'],
87
+ ["[1, 2].map(&:to_string)", '["1", "2"]'],
88
+ ["[1].each do end", "[1]"],
89
+ ["[[true]]", "[[true]]"],
90
+ ["[]", "[]"],
91
+ ["\r\n", "nothing"],
92
+ ["a = 0\nuntil a > 10 a += 1 end a", "11"],
93
+ ["a = 0\nwhile a < 10 a += 1 end a", "10"],
94
+ ["a = 1 3.times { a += 1 } a", "4"],
95
+ ["a = 1 a *= 2 a", "2"],
96
+ ["a = 1 a += 1 a", "2"],
39
97
  ["a = 1 a += 1 a", "2"],
40
98
  ["a = 1 a -= 1 a", "0"],
41
- ["a = 1 a *= 2 a", "2"],
42
99
  ["a = 1 a /= 2 a", "0.5"],
43
- ["a = 10 a %= 2 a", "0"],
44
- ["a = 1 a >>= 1 a", "0"],
45
100
  ["a = 1 a <<= 1 a", "2"],
46
- ["a = 1 a |= 2 a", "3"],
101
+ ["a = 1 a >>= 1 a", "0"],
47
102
  ["a = 1 a ^= 2 a", "3"],
48
- ["a = false a ||= true a", "true"],
103
+ ["a = 1 a |= 2 a", "3"],
104
+ ["a = 1 a", "1"],
105
+ ["a = 1 b = 2 c = a + b c", "3"],
106
+ ["a = 1", "1"],
107
+ ["a = 10 a %= 2 a", "0"],
108
+ ["a = b = c = 1 a + b + c", "3"],
49
109
  ["a = false a &&= true a", "false"],
50
- ["1 == 1", "true"],
51
- ["1 == 1 and 2 == 2", "true"],
52
- ["1 > 2", "false"],
53
- ["1 < 2", "true"],
54
- ["1 <= 1", "true"],
55
- ["1 >= 1", "true"],
56
- ["(true false)", "false"],
57
- ["if true 1", "1"],
58
- ["unless false 1", "1"],
59
- ["if false 1", "nothing"],
60
- ["unless true 1", "nothing"],
110
+ ["a = false a ||= true a", "true"],
111
+ ["a rescue :oops", ":oops"],
112
+ ["false ? 1 : 2", "2"],
113
+ ["false ? 1", ""],
114
+ ["false || false", "false"],
61
115
  ["if false 1 else 2", "2"],
62
- ["if false 1 elsif true 2", "2"],
63
- ["if false 1 elsif false 2", "nothing"],
64
- ["if false 1 else if true 2", "2"],
65
116
  ["if false 1 else if false 2", "nothing"],
117
+ ["if false 1 else if true 2", "2"],
66
118
  ["if false 1 else unless false 2", "2"],
67
119
  ["if false 1 else unless true 2", "nothing"],
68
- ["[]", "[]"],
69
- ["[1, 2, 3]", "[1, 2, 3]"],
70
- ["[1, 2, 3].include?(2)", "true"],
71
- ["[1, 2, 3].include?(4)", "false"],
72
- ["[1, 2, 3,].include?(4)", "false"],
73
- ["[1, 2, 3, \n ].include?(4)", "false"],
74
- ["[1].each do end", "[1]"],
75
- ["[,].include?(4)", "false"],
76
- ["[,,].include?(4)", "false"],
77
- ["[[true]]", "[[true]]"],
78
- ["2 * 3", "6"],
79
- ["2 * 3 + 2", "8"],
80
- ["2 + 2 * 3", "8"],
81
- ["2 / 4", "0.5"],
82
- ["4 % 3", "1"],
83
- %w[!false true],
84
- %w[!!true true],
85
- %w[!!1 true],
86
- %w[+1 1],
87
- %w[+1.0 1.0],
88
- %w[+true true],
89
- ["not true", "false"],
120
+ ["if false 1 elsif false 2", "nothing"],
121
+ ["if false 1 elsif true 2", "2"],
122
+ ["if false 1", "nothing"],
123
+ ["if true 1", "1"],
90
124
  ["not not false", "false"],
91
- %w[0 0],
92
- %w[1.1 1.1],
93
- %w[0x10 16],
94
- %w[0o10 8],
95
- %w[0b10 2],
96
- %w[1e1 10],
97
- %w[1.0e1 10.0],
98
- %w[10e1.0 100.0],
99
- ["true or false", "true"],
100
- ["true and false", "false"],
125
+ ["not true", "false"],
101
126
  ["random = 1 random", "1"],
127
+ ["true && false", "false"],
128
+ ["true && true", "true"],
129
+ ["true ? 1 : 2", "1"],
130
+ ["true ? 1", "1"],
131
+ ["true and false", "false"],
132
+ ["true or false", "true"],
102
133
  ["true || false", "true"],
103
- ["false || false", "false"],
104
- ["2 ** 3", "8"],
105
- ["2 ** 3 ** 2", "512"],
106
- ["a rescue :oops", ":oops"],
107
- ["[1, 2].map(&:to_string)", "[:1, :2]"],
108
- %w['' ""],
109
- %w["" ''],
110
- %w[:hello :hello],
111
- %w[:admin? :admin?],
112
- %w[:update!.to_string :update!],
113
- ["'Hello Dorian'", '"Hello Dorian"'],
134
+ ["unless false 1", "1"],
135
+ ["unless true 1", "nothing"],
136
+ ["until true", "nothing"],
137
+ ["while false", "nothing"],
138
+ ["{ a: 1, b: 2, c: 3 }", '{"a" => 1, "b" => 2, "c" => 3}'],
114
139
  ['"Hello Dorian"', '"Hello Dorian"'],
115
- ["'Hello \\{name}'", ':Hello + " \\{name}"'],
116
140
  ['"Hello \\{name}"', '"Hello \\{" + "name}"'],
117
- ["'Hello {1}'", '"Hello 1"'],
118
141
  ['"Hello {1}"', '"Hello 1"'],
119
- %w[:Hello.include?(:H) true],
120
- %w["Hello".downcase :hello],
121
- ["true ? 1", "1"],
122
- ["false ? 1", ""],
123
- ["true ? 1 : 2", "1"],
124
- ["false ? 1 : 2", "2"],
125
- %w[-1 -1],
126
- %w[--1 1],
127
- %w[-1.0 -1.0],
128
- %w[--1.0 1.0],
129
- ["while false", "nothing"],
130
- ["a = 0\nwhile a < 10 a += 1 end a", "10"],
131
- ["until true", "nothing"],
132
- ["a = 0\nuntil a > 10 a += 1 end a", "11"],
133
- ["1 >> 1", "0"],
134
- ["1 << 1", "2"],
135
- ["[1, 2].map(&:to_string)", '["1", "2"]']
142
+ ['user = {} user.name = "Dorian" user.name', ":Dorian"],
143
+ ['user = {} user[:name] = "Dorian" user[:name]', ":Dorian"],
144
+ ['{ "first_name": "Dorian" }', '{"first_name" => "Dorian"}'],
136
145
  ].each do |input, expected|
137
146
  it "#{input} == #{expected}" do
138
147
  expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
@@ -191,9 +200,16 @@ RSpec.describe Code do
191
200
  end
192
201
 
193
202
  [
203
+ "1.day.ago",
204
+ "1.day.from_now",
205
+ "1.hour.ago",
194
206
  "1.hour.from_now",
207
+ "2.days.ago",
208
+ "2.days.from_now",
209
+ "2.hours.ago",
210
+ "2.hours.from_now",
211
+ "Date.tomorrow",
195
212
  "Time.tomorrow",
196
- "Date.tomorrow"
197
213
  ].each do |input|
198
214
  it(input) { Code.evaluate(input) }
199
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié