code-ruby 1.8.3 → 1.8.5

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: 90ea1b00e6d1977fe3d8dfa40d26a7354556380bcd64c0c6cbe448a4ff3b28cd
4
- data.tar.gz: df66b284a124cf8ec9eedb572567920093c36227d5d0c9cc7c5b95ea59e487ea
3
+ metadata.gz: 2fece707e7c4b6ac1d7b25424dde96b9095c088b4f22a28e64af1b093b1a9a94
4
+ data.tar.gz: 960c8660fd9f5039ea34dec5b34d618da17b861e29a7b317ba4f701f5e6f2c26
5
5
  SHA512:
6
- metadata.gz: d1649988e887c986755e1ec2e9dbda9a5d410b290b0c29f3e025c33e6d229b79eb5905b390790a0fc287bb528a0a3a22c67e1650ad0982605594e10441404c7e
7
- data.tar.gz: d29cfd0814037087f57aaeb4bd1ef56e8f2755cf4f6eba38a6ab0f65c66ff8d780d87315490171fabb01354029acd5d0b962b546f2c66d7bdd30914b03f2ea2b
6
+ metadata.gz: 8324362cd96bf8134b82cf175afd91a4ff3f44c21c2dcaa9ae47778e175d491bfa923859e9fd74f7fd2eeb316ab0685e6b06a167c6d9f6e9f30d344fd5d5860a
7
+ data.tar.gz: edb4cc8af5291c3778ea5eca8d95b420b4555a29298a5b1132b2a2adfd2d599c06e142f2b76607d54ca058ed35d9640744343416d4ef386944814681d29e47e4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.8.3)
4
+ code-ruby (1.8.5)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.8.3
1
+ 1.8.5
@@ -142,6 +142,9 @@ class Code
142
142
  when "<<", "append"
143
143
  sig(args) { Object }
144
144
  code_append(code_value)
145
+ when "+", "plus"
146
+ sig(args) { List.maybe }
147
+ code_arguments.any? ? code_plus(code_value) : code_self
145
148
  when "-", "minus"
146
149
  sig(args) { List }
147
150
  code_minus(code_value)
@@ -227,8 +230,8 @@ class Code
227
230
  sig(args)
228
231
  code_sum
229
232
  when "uniq"
230
- sig(args)
231
- code_uniq
233
+ sig(args) { (Function | Class).maybe }
234
+ code_uniq(code_value, **globals)
232
235
  when "many?"
233
236
  sig(args)
234
237
  code_many?
@@ -580,6 +583,12 @@ class Code
580
583
  self
581
584
  end
582
585
 
586
+ def code_plus(other)
587
+ code_other = other.to_code
588
+
589
+ List.new(raw + code_other.raw)
590
+ end
591
+
583
592
  def code_minus(other)
584
593
  code_other = other.to_code
585
594
 
@@ -1001,8 +1010,39 @@ class Code
1001
1010
  Integer.new(raw.size)
1002
1011
  end
1003
1012
 
1004
- def code_uniq
1005
- List.new(raw.uniq)
1013
+ def code_uniq(argument = nil, **globals)
1014
+ code_argument = argument.to_code
1015
+
1016
+ unless code_argument.is_a?(Function) || code_argument.is_a?(Class)
1017
+ return List.new(raw.uniq)
1018
+ end
1019
+
1020
+ if code_argument.is_a?(Class)
1021
+ return List.new(
1022
+ raw
1023
+ .select { |code_element| code_element.is_a?(code_argument.raw) }
1024
+ .uniq
1025
+ )
1026
+ end
1027
+
1028
+ index = 0
1029
+
1030
+ List.new(
1031
+ raw.uniq do |code_element|
1032
+ if code_argument.is_a?(Function)
1033
+ code_argument
1034
+ .call(
1035
+ arguments: List.new([code_element, Integer.new(index), self]),
1036
+ **globals
1037
+ )
1038
+ .tap { index += 1 }
1039
+ else
1040
+ code_element.tap { index += 1 }
1041
+ end
1042
+ rescue Error::Next => e
1043
+ e.code_value.tap { index += 1 }
1044
+ end
1045
+ )
1006
1046
  end
1007
1047
 
1008
1048
  def code_sum
@@ -74,6 +74,17 @@ class Code
74
74
  self.raw = args.first.to_time.in_time_zone(::Time.zone)
75
75
  elsif args.first.is_a?(::ActiveSupport::TimeWithZone)
76
76
  self.raw = args.first.dup
77
+ elsif args.first.is_a?(Integer) || args.first.is_a?(Decimal) ||
78
+ args.first.is_a?(::Integer) || args.first.is_a?(::Float) ||
79
+ args.first.is_a?(::BigDecimal)
80
+ code_value = args.first.to_code
81
+ timestamp =
82
+ if code_value.is_a?(Decimal)
83
+ code_value.raw.to_r
84
+ else
85
+ code_value.raw
86
+ end
87
+ self.raw = ::Time.zone.at(timestamp)
77
88
  else
78
89
  self.raw = ::Time.zone.now
79
90
  end
@@ -3,7 +3,14 @@
3
3
  require "spec_helper"
4
4
 
5
5
  RSpec.describe Code::Object::List do
6
- [["[] == []", "true"], ["[1, 2, 3].sum", "6"]].each do |input, expected|
6
+ [
7
+ ["[] == []", "true"],
8
+ ["[1, 2, 3].sum", "6"],
9
+ ["[1, 2] + [3, 4]", "[1, 2, 3, 4]"],
10
+ ["[] + []", "[]"],
11
+ ["[1].any", "true"],
12
+ ["[].any", "false"]
13
+ ].each do |input, expected|
7
14
  it "#{input} == #{expected}" do
8
15
  expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
9
16
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ RSpec.describe Code::Object::Time do
6
+ [
7
+ ['Time.new(1).format("%s")', ":1"],
8
+ ['Time.new(1100042342).format("%s")', ":1100042342"],
9
+ ['Time.new(1.2).format("%s.%L")', '"1.200"'],
10
+ ['Time.new(11212.1212).format("%s.%L")', '"11212.121"']
11
+ ].each do |input, expected|
12
+ it "#{input} == #{expected}" do
13
+ expect(Code.evaluate(input)).to eq(Code.evaluate(expected))
14
+ end
15
+ end
16
+ end
data/spec/code_spec.rb CHANGED
@@ -284,6 +284,9 @@ RSpec.describe Code do
284
284
  ["[1, 2, 3].map { |i| next(0) if i.even? i ** 2}", "[1, 0, 9]"],
285
285
  ["[1, 2, 3].select { |n| n.even? }", "[2]"],
286
286
  ["[1, 2, 3].select { |n| n.even? }.select { |n| n.odd? }", "[]"],
287
+ ["[1, 2, 3, 4].uniq { |n| n % 2 }", "[1, 2]"],
288
+ ['[1, "1", 2, "2"].uniq(String)', '["1", "2"]'],
289
+ ["[1, 2, 3, 4].uniq(String)", "[]"],
287
290
  ["[1, 2].map(&:to_string)", "[:1, :2]"],
288
291
  ["[1, 2].map(&:to_string)", '["1", "2"]'],
289
292
  ["[1, 2].map(&:to_string)", '["1", "2"]'],
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: 1.8.3
4
+ version: 1.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -345,6 +345,7 @@ files:
345
345
  - spec/code/object/nothing_spec.rb
346
346
  - spec/code/object/range_spec.rb
347
347
  - spec/code/object/string_spec.rb
348
+ - spec/code/object/time_spec.rb
348
349
  - spec/code/parser/boolean_spec.rb
349
350
  - spec/code/parser/chained_call_spec.rb
350
351
  - spec/code/parser/dictionary_spec.rb