code-ruby 1.2.3 → 1.2.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: f7e9396ebb09849d887bc18eb7069427616992d307fa3a556aa24fb330c4c8bf
4
- data.tar.gz: b97e8a8c3bacbbed9dd14f5b3de85b11a20db9e66e1974e3bfdb1b2e543227a4
3
+ metadata.gz: dc5cea2598154d67fcc651c39a7bfeee5b573fa52aadc4d5f975534c578ef887
4
+ data.tar.gz: 5f22af50a64fba0be71573c48a5e3bcec67a75de558c239062646d6c640737c1
5
5
  SHA512:
6
- metadata.gz: 0c3e38a8a8eaeca2aa543c8128ad083a62f2b93a6b7d32efaf9efbb02a315df957b3ae6516031ec22edc573c3fccc3cd4347ffc582ab4083410b01a1ab2bf117
7
- data.tar.gz: 21ad77b0775d8342799f5fbad810fc52e96b4c3d4bfc25154d614afc6fbd8c1561f1ac85cc4be5adf65e96337bb255585ea8449e5dcd6957318ea171f2b9b739
6
+ metadata.gz: 9b20becc62e2a60a82913b8a3d9d19132c0ec9df971fe294b716646da21b3b51578536234d365b18d67cb7692f9743067091c99daaff5493804cdb676d6eea97
7
+ data.tar.gz: c18685983db2eb1995ded8f30161ead10e941d43fd164a8e13b0c45afd09d9d8cf41a53e3f41b9d994ac957c190666dbe37444dcdd45d64621f662463c640701
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (1.2.3)
4
+ code-ruby (1.2.5)
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.3
1
+ 1.2.5
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
 
@@ -94,7 +94,7 @@ class Code
94
94
  {
95
95
  year: (String | Integer).maybe,
96
96
  month: (String | Integer).maybe,
97
- day: (String | Integer).maybe,
97
+ day: (String | Integer).maybe
98
98
  }
99
99
  end
100
100
 
@@ -104,7 +104,7 @@ class Code
104
104
  code_change(
105
105
  year: code_value.code_get(:year),
106
106
  month: code_value.code_get(:month),
107
- day: code_value.code_get(:day),
107
+ day: code_value.code_get(:day)
108
108
  )
109
109
  end
110
110
  else
@@ -141,11 +141,13 @@ class Code
141
141
  code_month = month.to_code
142
142
  code_day = day.to_code
143
143
 
144
- raw.change(**{
145
- year: code_year.raw,
146
- month: code_month.raw,
147
- day: code_day.raw
148
- }.compact)
144
+ raw.change(
145
+ **{
146
+ year: code_year.raw,
147
+ month: code_month.raw,
148
+ day: code_day.raw
149
+ }.compact
150
+ )
149
151
 
150
152
  self
151
153
  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,19 @@
3
3
  class Code
4
4
  class Object
5
5
  class Html < Object
6
+ def initialize(*args, **_kargs, &)
7
+ @raw =
8
+ if args.first.is_an?(Html)
9
+ args.first.raw
10
+ elsif args.first.is_a?(::Nokogiri::XML::NodeSet)
11
+ args.first
12
+ elsif args.first.is_a?(Nokogiri::XML::Node)
13
+ args.first
14
+ else
15
+ Nokogiri.HTML(args.first.to_s)
16
+ end
17
+ end
18
+
6
19
  def self.call(**args)
7
20
  code_operator = args.fetch(:operator, nil).to_code
8
21
  code_arguments = args.fetch(:arguments, []).to_code
@@ -35,6 +48,52 @@ class Code
35
48
 
36
49
  String.new(CGI.escapeHTML(string.to_s))
37
50
  end
51
+
52
+ def call(**args)
53
+ code_operator = args.fetch(:operator, nil).to_code
54
+ code_arguments = args.fetch(:arguments, []).to_code
55
+ code_value = code_arguments.code_first
56
+ globals = multi_fetch(args, *GLOBALS)
57
+
58
+ case code_operator.to_s
59
+ when "css"
60
+ sig(args) { String }
61
+ code_css(code_value)
62
+ when "map"
63
+ sig(args) { Function }
64
+ code_map(code_value, **globals)
65
+ when "to_string"
66
+ sig(args)
67
+ code_to_string
68
+ else
69
+ super
70
+ end
71
+ end
72
+
73
+ def code_css(query)
74
+ code_query = query.to_code
75
+
76
+ Html.new(raw.css(code_query.raw))
77
+ end
78
+
79
+ def code_map(argument, **globals)
80
+ code_argument = argument.to_code
81
+
82
+ List.new(
83
+ raw.map.with_index do |element, index|
84
+ code_argument.call(
85
+ arguments: List.new([element.to_code, Integer.new(index), self]),
86
+ **globals
87
+ )
88
+ rescue Error::Next => e
89
+ e.code_value
90
+ end
91
+ )
92
+ end
93
+
94
+ def code_to_string
95
+ String.new(raw.text)
96
+ end
38
97
  end
39
98
  end
40
99
  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)
@@ -109,17 +115,15 @@ class Code
109
115
  Boolean.new(
110
116
  raw.any? do |code_element|
111
117
  if code_argument.nothing?
112
- true
118
+ true.tap { index += 1 }
113
119
  else
114
120
  code_argument.call(
115
121
  arguments: List.new([code_element, Integer.new(index), self]),
116
122
  **globals
117
- ).truthy?
123
+ ).truthy?.tap { index += 1 }
118
124
  end
119
125
  rescue Error::Next => e
120
- e.code_value
121
- ensure
122
- index += 1
126
+ e.code_value.tap { index += 1 }
123
127
  end
124
128
  )
125
129
  end
@@ -265,17 +269,15 @@ class Code
265
269
  Boolean.new(
266
270
  raw.none? do |code_element|
267
271
  if code_argument.nothing?
268
- code_element.truthy?
272
+ code_element.truthy?.tap { index += 1 }
269
273
  else
270
274
  code_argument.call(
271
275
  arguments: List.new([code_element, Integer.new(index), self]),
272
276
  **globals
273
- ).truthy?
277
+ ).truthy?.tap { index += 1 }
274
278
  end
275
279
  rescue Error::Next => e
276
- e.code_value.truthy?
277
- ensure
278
- index += 1
280
+ e.code_value.truthy?.tap { index += 1 }
279
281
  end
280
282
  )
281
283
  end
@@ -298,6 +300,48 @@ class Code
298
300
  List.new(raw.reverse)
299
301
  end
300
302
 
303
+ def code_compact(argument = nil, **globals)
304
+ code_argument = argument.to_code
305
+
306
+ index = 0
307
+
308
+ List.new(
309
+ raw.select do |code_element|
310
+ if code_argument.nothing?
311
+ code_element.truthy?.tap { index += 1 }
312
+ else
313
+ code_argument.call(
314
+ arguments: List.new([code_element, Integer.new(index), self]),
315
+ **globals
316
+ ).truthy?.tap { index += 1 }
317
+ end
318
+ rescue Error::Next => e
319
+ e.code_value.truhty?.tap { index += 1 }
320
+ end
321
+ )
322
+ end
323
+
324
+ def code_compact!(argument = nil, **globals)
325
+ code_argument = argument.to_code
326
+
327
+ index = 0
328
+
329
+ raw.select! do |code_element|
330
+ if code_argument.nothing?
331
+ code_element.truthy?.tap { index += 1 }
332
+ else
333
+ code_argument.call(
334
+ arguments: List.new([code_element, Integer.new(index), self]),
335
+ **globals
336
+ ).truthy?.tap { index += 1 }
337
+ end
338
+ rescue Error::Next => e
339
+ e.code_value.truhty?.tap { index += 1 }
340
+ end
341
+
342
+ self
343
+ end
344
+
301
345
  def code_select(argument, **globals)
302
346
  code_argument = argument.to_code
303
347
 
@@ -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
@@ -68,9 +68,45 @@ class Code
68
68
  when "future?"
69
69
  sig(args)
70
70
  code_future?
71
+ when "year"
72
+ sig(args)
73
+ code_year
74
+ when "years"
75
+ sig(args)
76
+ code_years
77
+ when "month"
78
+ sig(args)
79
+ code_month
80
+ when "months"
81
+ sig(args)
82
+ code_months
83
+ when "day"
84
+ sig(args)
85
+ code_day
86
+ when "days"
87
+ sig(args)
88
+ code_days
71
89
  when "hour"
72
90
  sig(args)
73
91
  code_hour
92
+ when "hours"
93
+ sig(args)
94
+ code_hours
95
+ when "minute"
96
+ sig(args)
97
+ code_minute
98
+ when "minutes"
99
+ sig(args)
100
+ code_minutes
101
+ when "second"
102
+ sig(args)
103
+ code_second
104
+ when "seconds"
105
+ sig(args)
106
+ code_seconds
107
+ when "format"
108
+ sig(args) { String }
109
+ code_format(code_value)
74
110
  else
75
111
  super
76
112
  end
@@ -98,9 +134,59 @@ class Code
98
134
  code_after?
99
135
  end
100
136
 
137
+ def code_year
138
+ Integer.new(raw.year)
139
+ end
140
+
141
+ def code_years
142
+ Integer.new(raw.year)
143
+ end
144
+
145
+ def code_month
146
+ Integer.new(raw.month)
147
+ end
148
+
149
+ def code_months
150
+ Integer.new(raw.month)
151
+ end
152
+
153
+ def code_day
154
+ Integer.new(raw.day)
155
+ end
156
+
157
+ def code_days
158
+ Integer.new(raw.day)
159
+ end
160
+
101
161
  def code_hour
102
162
  Integer.new(raw.hour)
103
163
  end
164
+
165
+ def code_hours
166
+ Integer.new(raw.hour)
167
+ end
168
+
169
+ def code_minute
170
+ Integer.new(raw.min)
171
+ end
172
+
173
+ def code_minutes
174
+ Integer.new(raw.min)
175
+ end
176
+
177
+ def code_second
178
+ Integer.new(raw.sec)
179
+ end
180
+
181
+ def code_seconds
182
+ Integer.new(raw.sec)
183
+ end
184
+
185
+ def code_format(format)
186
+ code_format = format.to_code
187
+
188
+ String.new(raw.strftime(code_format.raw))
189
+ end
104
190
  end
105
191
  end
106
192
  end
@@ -27,7 +27,7 @@ class Code
27
27
  (
28
28
  statement.aka(:left) << (
29
29
  left_square_bracket << whitespace? << Statement <<
30
- (whitespace? << right_square_bracket).maybe
30
+ (whitespace? << right_square_bracket).maybe
31
31
  ).repeat(1).aka(:statements).maybe
32
32
  )
33
33
  .aka(:square_bracket)
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,13 @@ class Hash
136
137
  Code::Object::Dictionary.new(self)
137
138
  end
138
139
  end
140
+
141
+ module Nokogiri
142
+ module XML
143
+ class Element
144
+ def to_code
145
+ Code::Object::Html.new(self)
146
+ end
147
+ end
148
+ end
149
+ end
data/spec/code_spec.rb CHANGED
@@ -5,6 +5,18 @@ require "spec_helper"
5
5
  RSpec.describe Code do
6
6
  (
7
7
  %w[
8
+ Time.now.second
9
+ Time.now.seconds
10
+ Time.now.minute
11
+ Time.now.minutes
12
+ Time.now.hour
13
+ Time.now.hours
14
+ Time.now.day
15
+ Time.now.days
16
+ Time.now.month
17
+ Time.now.months
18
+ Time.now.year
19
+ Time.now.years
8
20
  1.day.ago
9
21
  1.day.from_now
10
22
  1.hour.ago
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-03 00:00:00.000000000 Z
10
+ date: 2025-01-04 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport
@@ -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