code-ruby 1.2.2 → 1.2.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: 1aa07641fb5e7daf2c529f4ec5abb083d9401de81e49b1f18b01124cab01d82e
4
- data.tar.gz: 7e40438c2b564a9f6e97f334a32509759fdfae25f6b81930431ba6ff0297ade3
3
+ metadata.gz: 90f22080323e543a0af569f126910e440deafe34ae8c8e44684a22cb5c4e6339
4
+ data.tar.gz: 5bd9cb88112781491172e9239aea326dbd92c0b5dc2d42b7b2aebb2cc5fb1cdc
5
5
  SHA512:
6
- metadata.gz: f3cb329cbc9ec183c8c15e9cc07256411a56d4b10af3219c2fceb1b55e2ec64c5ad31248f46a4e7a52c374b1cf1e3f68193e12ae15d45a06bf27ba396d4b013a
7
- data.tar.gz: 7953bf969f8f53321260ad044936e2d02f864f1c65361cd87da81d573d7c91ff2c5ebacb6435fb33de134322ed94aaefed23fc0111280cbcfdacf7c8c96516d4
6
+ metadata.gz: 2e5f00fad4846b46233f20b9ef29c21af07bdd7dec262b68cdf19bd60e143624f1d6336ba4d0e09ed71661f1ad8a48f57b4d2857ad4a29904369b9d7f376d600
7
+ data.tar.gz: b0b61d5324f112a5a19d8a981bbfe91ce5c77be30a1b246b7143e62b7585d0cfbc4baa8665f32c8c579520ddfc29208d43169414ef2463f8c6516f8b03cdc937
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.2.2)
4
+ code-ruby (1.2.4)
5
5
  activesupport
6
6
  base64
7
7
  bigdecimal
@@ -10,6 +10,7 @@ PATH
10
10
  json
11
11
  language-ruby
12
12
  net-http
13
+ nokogiri
13
14
  uri
14
15
  zeitwerk
15
16
 
@@ -101,6 +102,7 @@ GEM
101
102
  libv8-node (18.19.0.0-arm64-darwin)
102
103
  libv8-node (18.19.0.0-x86_64-linux)
103
104
  logger (1.6.4)
105
+ mini_portile2 (2.8.8)
104
106
  mini_racer (0.16.0)
105
107
  libv8-node (~> 18.19.0.0)
106
108
  minitest (5.25.4)
@@ -108,6 +110,13 @@ GEM
108
110
  ruby2_keywords (~> 0.0.1)
109
111
  net-http (0.6.0)
110
112
  uri
113
+ nokogiri (1.18.1)
114
+ mini_portile2 (~> 2.8.2)
115
+ racc (~> 1.4)
116
+ nokogiri (1.18.1-arm64-darwin)
117
+ racc (~> 1.4)
118
+ nokogiri (1.18.1-x86_64-linux-gnu)
119
+ racc (~> 1.4)
111
120
  openssl (3.2.1)
112
121
  ostruct (0.6.1)
113
122
  parallel (1.26.3)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.2.4
data/code-ruby.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency "json"
22
22
  s.add_dependency "language-ruby"
23
23
  s.add_dependency "net-http"
24
+ s.add_dependency "nokogiri"
24
25
  s.add_dependency "uri"
25
26
  s.add_dependency "zeitwerk"
26
27
 
@@ -22,6 +22,9 @@ class Code
22
22
  when "today"
23
23
  sig(args)
24
24
  code_today
25
+ when "current"
26
+ sig(args)
27
+ code_current
25
28
  when "now"
26
29
  sig(args)
27
30
  code_now
@@ -47,6 +50,11 @@ class Code
47
50
  new(::Time.zone.now.beginning_of_day)
48
51
  end
49
52
 
53
+ def self.code_current
54
+ ::Time.zone ||= Time::DEFAULT_ZONE
55
+ new(::Time.zone.now.beginning_of_day)
56
+ end
57
+
50
58
  def self.code_tomorrow
51
59
  ::Time.zone ||= Time::DEFAULT_ZONE
52
60
  new(::Time.zone.tomorrow.beginning_of_day)
@@ -59,6 +67,8 @@ class Code
59
67
 
60
68
  def call(**args)
61
69
  code_operator = args.fetch(:operator, nil).to_code
70
+ code_arguments = args.fetch(:arguments, []).to_code
71
+ code_value = code_arguments.code_first
62
72
 
63
73
  case code_operator.to_s
64
74
  when "hour"
@@ -79,6 +89,24 @@ class Code
79
89
  when "seconds"
80
90
  sig(args)
81
91
  code_seconds
92
+ when "change"
93
+ sig(args) do
94
+ {
95
+ year: (String | Integer).maybe,
96
+ month: (String | Integer).maybe,
97
+ day: (String | Integer).maybe
98
+ }
99
+ end
100
+
101
+ if code_value.nothing?
102
+ code_change
103
+ else
104
+ code_change(
105
+ year: code_value.code_get(:year),
106
+ month: code_value.code_get(:month),
107
+ day: code_value.code_get(:day)
108
+ )
109
+ end
82
110
  else
83
111
  super
84
112
  end
@@ -107,6 +135,22 @@ class Code
107
135
  def code_seconds
108
136
  Integer.new(0)
109
137
  end
138
+
139
+ def code_change(year: nil, month: nil, day: nil)
140
+ code_year = year.to_code
141
+ code_month = month.to_code
142
+ code_day = day.to_code
143
+
144
+ raw.change(
145
+ **{
146
+ year: code_year.raw,
147
+ month: code_month.raw,
148
+ day: code_day.raw
149
+ }.compact
150
+ )
151
+
152
+ self
153
+ end
110
154
  end
111
155
  end
112
156
  end
@@ -396,7 +396,7 @@ class Code
396
396
 
397
397
  def code_flatten(level = nil)
398
398
  code_level = level.to_code
399
- code_level = Integer.new(-1) if level.nothing?
399
+ code_level = Integer.new(-1) if code_level.nothing?
400
400
  code_to_list.code_flatten(code_level)
401
401
  end
402
402
 
@@ -3,6 +3,18 @@
3
3
  class Code
4
4
  class Object
5
5
  class Html < Object
6
+ def initialize(*args, **_kargs, &)
7
+ if args.first.is_an?(Html)
8
+ @raw = args.first.raw
9
+ elsif args.first.is_a?(::Nokogiri::XML::NodeSet)
10
+ @raw = args.first
11
+ elsif args.first.is_a?(Nokogiri::XML::Node)
12
+ @raw = args.first
13
+ else
14
+ @raw = Nokogiri::HTML(args.first.to_s)
15
+ end
16
+ end
17
+
6
18
  def self.call(**args)
7
19
  code_operator = args.fetch(:operator, nil).to_code
8
20
  code_arguments = args.fetch(:arguments, []).to_code
@@ -35,6 +47,52 @@ class Code
35
47
 
36
48
  String.new(CGI.escapeHTML(string.to_s))
37
49
  end
50
+
51
+ def call(**args)
52
+ code_operator = args.fetch(:operator, nil).to_code
53
+ code_arguments = args.fetch(:arguments, []).to_code
54
+ code_value = code_arguments.code_first
55
+ globals = multi_fetch(args, *GLOBALS)
56
+
57
+ case code_operator.to_s
58
+ when "css"
59
+ sig(args) { String }
60
+ code_css(code_value)
61
+ when "map"
62
+ sig(args) { Function }
63
+ code_map(code_value, **globals)
64
+ when "to_string"
65
+ sig(args)
66
+ code_to_string
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ def code_css(query)
73
+ code_query = query.to_code
74
+
75
+ Html.new(raw.css(code_query.raw))
76
+ end
77
+
78
+ def code_map(argument, **globals)
79
+ code_argument = argument.to_code
80
+
81
+ List.new(
82
+ raw.map.with_index do |element, index|
83
+ code_argument.call(
84
+ arguments: List.new([element.to_code, Integer.new(index), self]),
85
+ **globals
86
+ )
87
+ rescue Error::Next => e
88
+ e.code_value
89
+ end
90
+ )
91
+ end
92
+
93
+ def code_to_string
94
+ String.new(raw.text)
95
+ end
38
96
  end
39
97
  end
40
98
  end
@@ -81,6 +81,12 @@ class Code
81
81
  when "select!"
82
82
  sig(args) { Function }
83
83
  code_select!(code_value, **globals)
84
+ when "compact"
85
+ sig(args) { Function.maybe }
86
+ code_compact(code_value, **globals)
87
+ when "compact!"
88
+ sig(args) { Function.maybe }
89
+ code_compact!(code_value, **globals)
84
90
  when "reject"
85
91
  sig(args) { Function }
86
92
  code_reject(code_value, **globals)
@@ -298,6 +304,52 @@ class Code
298
304
  List.new(raw.reverse)
299
305
  end
300
306
 
307
+ def code_compact(argument = nil, **globals)
308
+ code_argument = argument.to_code
309
+
310
+ index = 0
311
+
312
+ List.new(
313
+ raw.compact do |code_element|
314
+ if code_argument.nothing?
315
+ code_element.falsy?
316
+ else
317
+ code_argument.call(
318
+ arguments: List.new([code_element, Integer.new(index), self]),
319
+ **globals
320
+ ).falsy?
321
+ end
322
+ rescue Error::Next => e
323
+ e.code_value.falsy?
324
+ ensure
325
+ index += 1
326
+ end
327
+ )
328
+ end
329
+
330
+ def code_compact!(argument = nil, **globals)
331
+ code_argument = argument.to_code
332
+
333
+ index = 0
334
+
335
+ raw.compact! do |code_element|
336
+ if code_argument.nothing?
337
+ code_element.falsy?
338
+ else
339
+ code_argument.call(
340
+ arguments: List.new([code_element, Integer.new(index), self]),
341
+ **globals
342
+ ).falsy?
343
+ end
344
+ rescue Error::Next => e
345
+ e.code_value.falsy?
346
+ ensure
347
+ index += 1
348
+ end
349
+
350
+ self
351
+ end
352
+
301
353
  def code_select(argument, **globals)
302
354
  code_argument = argument.to_code
303
355
 
@@ -31,6 +31,9 @@ class Code
31
31
  when "include?"
32
32
  sig(args) { String }
33
33
  code_include?(code_value)
34
+ when "first"
35
+ sig(args) { Integer.maybe }
36
+ code_first(code_value)
34
37
  when "reverse"
35
38
  sig(args)
36
39
  code_reverse
@@ -65,6 +68,12 @@ class Code
65
68
  def code_to_function(**_globals)
66
69
  Function.new([{ name: "_" }], "_.#{raw}")
67
70
  end
71
+
72
+ def code_first(n = nil)
73
+ code_n = n.to_code
74
+ code_n = Integer.new(1) if code_n.nothing?
75
+ String.new(raw.first(code_n.raw))
76
+ end
68
77
  end
69
78
  end
70
79
  end
@@ -71,6 +71,9 @@ class Code
71
71
  when "hour"
72
72
  sig(args)
73
73
  code_hour
74
+ when "format"
75
+ sig(args) { String }
76
+ code_format(code_value)
74
77
  else
75
78
  super
76
79
  end
@@ -101,6 +104,12 @@ class Code
101
104
  def code_hour
102
105
  Integer.new(raw.hour)
103
106
  end
107
+
108
+ def code_format(format)
109
+ code_format = format.to_code
110
+
111
+ String.new(raw.strftime(code_format.raw))
112
+ end
104
113
  end
105
114
  end
106
115
  end
@@ -7,10 +7,6 @@ class Code
7
7
  UnaryMinus
8
8
  end
9
9
 
10
- def square_bracket
11
- SquareBracket
12
- end
13
-
14
10
  def whitespace
15
11
  Whitespace
16
12
  end
@@ -30,7 +26,7 @@ class Code
30
26
  def root
31
27
  (
32
28
  statement.aka(:left) << (
33
- left_square_bracket << whitespace? << square_bracket <<
29
+ left_square_bracket << whitespace? << Statement <<
34
30
  (whitespace? << right_square_bracket).maybe
35
31
  ).repeat(1).aka(:statements).maybe
36
32
  )
data/lib/code-ruby.rb CHANGED
@@ -8,6 +8,7 @@ require "did_you_mean"
8
8
  require "json"
9
9
  require "language-ruby"
10
10
  require "net/http"
11
+ require "nokogiri"
11
12
  require "stringio"
12
13
  require "timeout"
13
14
  require "uri"
@@ -136,3 +137,9 @@ class Hash
136
137
  Code::Object::Dictionary.new(self)
137
138
  end
138
139
  end
140
+
141
+ class Nokogiri::XML::Element
142
+ def to_code
143
+ Code::Object::Html.new(self)
144
+ end
145
+ end
data/spec/code_spec.rb CHANGED
@@ -78,6 +78,7 @@ RSpec.describe Code do
78
78
  Json.parse('[]')
79
79
  Json.parse('{}')
80
80
  Json.parse('random-string')
81
+ {}["".to_string]
81
82
  ] + ["Time.hour >= 6 and Time.hour <= 23"]
82
83
  ).each { |input| it(input) { described_class.evaluate(input) } }
83
84
 
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.2.2
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -121,6 +121,20 @@ dependencies:
121
121
  - - ">="
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0'
124
+ - !ruby/object:Gem::Dependency
125
+ name: nokogiri
126
+ requirement: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :runtime
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
124
138
  - !ruby/object:Gem::Dependency
125
139
  name: uri
126
140
  requirement: !ruby/object:Gem::Requirement