code-ruby 3.0.6 → 3.0.8

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -0
  3. data/Gemfile.lock +1 -1
  4. data/VERSION +1 -1
  5. data/bin/code +27 -3
  6. data/lib/code/error.rb +10 -5
  7. data/lib/code/extensions/active_support.rb +9 -0
  8. data/lib/code/extensions/array.rb +7 -0
  9. data/lib/code/extensions/big_decimal.rb +9 -0
  10. data/lib/code/extensions/class.rb +7 -0
  11. data/lib/code/extensions/false_class.rb +7 -0
  12. data/lib/code/extensions/float.rb +9 -0
  13. data/lib/code/extensions/hash.rb +7 -0
  14. data/lib/code/extensions/integer.rb +9 -0
  15. data/lib/code/extensions/module.rb +7 -0
  16. data/lib/code/extensions/nil_class.rb +7 -0
  17. data/lib/code/extensions/nokogiri.rb +11 -0
  18. data/lib/code/extensions/object.rb +9 -0
  19. data/lib/code/extensions/string.rb +7 -0
  20. data/lib/code/extensions/symbol.rb +7 -0
  21. data/lib/code/extensions/true_class.rb +7 -0
  22. data/lib/code/extensions/word_number_comparaisons.rb +407 -0
  23. data/lib/code/format.rb +73 -56
  24. data/lib/code/node/code.rb +2 -1
  25. data/lib/code/node/statement.rb +4 -2
  26. data/lib/code/node/string.rb +1 -1
  27. data/lib/code/node/while.rb +8 -10
  28. data/lib/code/object/date.rb +37 -17
  29. data/lib/code/object/function.rb +23 -11
  30. data/lib/code/object/global.rb +5 -4
  31. data/lib/code/object/html.rb +169 -52
  32. data/lib/code/object/http.rb +8 -8
  33. data/lib/code/object/ics.rb +22 -18
  34. data/lib/code/object/identifier_list.rb +12 -10
  35. data/lib/code/object/list.rb +1 -5
  36. data/lib/code/object/super.rb +2 -1
  37. data/lib/code/object/time.rb +46 -44
  38. data/lib/code/parser.rb +373 -152
  39. data/lib/code-ruby.rb +16 -149
  40. data/spec/bin/code_spec.rb +32 -0
  41. data/spec/code/format_spec.rb +7 -10
  42. data/spec/code/node/call_spec.rb +1 -3
  43. data/spec/code/object/function_spec.rb +4 -8
  44. data/spec/code/object/http_spec.rb +33 -0
  45. data/spec/code/object/ics_spec.rb +5 -5
  46. data/spec/code/object/list_spec.rb +1 -1
  47. data/spec/code/parser_spec.rb +22 -0
  48. data/spec/code_spec.rb +347 -328
  49. metadata +18 -2
  50. data/applies +0 -0
@@ -4,15 +4,116 @@ class Code
4
4
  class Object
5
5
  class Html < Object
6
6
  TAGS = %w[
7
- a abbr address area article aside audio b base bdi bdo blockquote body br
8
- button canvas caption cite code col colgroup data datalist dd del details
9
- dfn dialog div dl dt em embed fieldset figcaption figure footer form h1
10
- h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins kbd
11
- label legend li link main map mark meta meter nav noscript object ol
12
- optgroup option output p picture pre progress q rp rt ruby s samp script
13
- section select slot small source span strong style sub summary sup table
14
- tbody td template textarea tfoot th thead time title tr track u ul var
15
- video wbr
7
+ a
8
+ abbr
9
+ address
10
+ area
11
+ article
12
+ aside
13
+ audio
14
+ b
15
+ base
16
+ bdi
17
+ bdo
18
+ blockquote
19
+ body
20
+ br
21
+ button
22
+ canvas
23
+ caption
24
+ cite
25
+ code
26
+ col
27
+ colgroup
28
+ data
29
+ datalist
30
+ dd
31
+ del
32
+ details
33
+ dfn
34
+ dialog
35
+ div
36
+ dl
37
+ dt
38
+ em
39
+ embed
40
+ fieldset
41
+ figcaption
42
+ figure
43
+ footer
44
+ form
45
+ h1
46
+ h2
47
+ h3
48
+ h4
49
+ h5
50
+ h6
51
+ head
52
+ header
53
+ hgroup
54
+ hr
55
+ html
56
+ i
57
+ iframe
58
+ img
59
+ input
60
+ ins
61
+ kbd
62
+ label
63
+ legend
64
+ li
65
+ link
66
+ main
67
+ map
68
+ mark
69
+ meta
70
+ meter
71
+ nav
72
+ noscript
73
+ object
74
+ ol
75
+ optgroup
76
+ option
77
+ output
78
+ p
79
+ picture
80
+ pre
81
+ progress
82
+ q
83
+ rp
84
+ rt
85
+ ruby
86
+ s
87
+ samp
88
+ script
89
+ section
90
+ select
91
+ slot
92
+ small
93
+ source
94
+ span
95
+ strong
96
+ style
97
+ sub
98
+ summary
99
+ sup
100
+ table
101
+ tbody
102
+ td
103
+ template
104
+ textarea
105
+ tfoot
106
+ th
107
+ thead
108
+ time
109
+ title
110
+ tr
111
+ track
112
+ u
113
+ ul
114
+ var
115
+ video
116
+ wbr
16
117
  ].freeze
17
118
 
18
119
  def initialize(*args, **_kargs, &_block)
@@ -58,7 +159,12 @@ class Code
58
159
  end
59
160
  end
60
161
 
61
- def self.code_tag(name, attributes_or_function = {}, function = nil, **globals)
162
+ def self.code_tag(
163
+ name,
164
+ attributes_or_function = {},
165
+ function = nil,
166
+ **globals
167
+ )
62
168
  code_name = name.to_code
63
169
 
64
170
  if attributes_or_function.is_a?(Function)
@@ -70,7 +176,8 @@ class Code
70
176
  end
71
177
 
72
178
  fragment = Nokogiri::HTML::DocumentFragment.parse("")
73
- node = Nokogiri::XML::Node.new(code_name.to_s.downcase, fragment.document)
179
+ node =
180
+ Nokogiri::XML::Node.new(code_name.to_s.downcase, fragment.document)
74
181
 
75
182
  code_attributes.raw.each do |code_key, code_value|
76
183
  next if code_key.nothing?
@@ -80,16 +187,18 @@ class Code
80
187
  end
81
188
 
82
189
  if code_function.something?
83
- code_content = code_function.call(
84
- arguments: List.new([code_name, code_attributes]),
85
- **globals
86
- )
190
+ code_content =
191
+ code_function.call(
192
+ arguments: List.new([code_name, code_attributes]),
193
+ **globals
194
+ )
87
195
 
88
- if code_content.is_an?(Html)
89
- content = Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
90
- else
91
- content = Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
92
- end
196
+ content =
197
+ if code_content.is_an?(Html)
198
+ Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
199
+ else
200
+ Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
201
+ end
93
202
 
94
203
  node.add_child(content)
95
204
  end
@@ -100,21 +209,23 @@ class Code
100
209
  end
101
210
 
102
211
  def self.code_escape(value_or_function = nil, **globals)
103
- if value_or_function.is_a?(Function)
104
- code_value = value_or_function.to_code.call(**globals)
105
- else
106
- code_value = value_or_function.to_code
107
- end
212
+ code_value =
213
+ if value_or_function.is_a?(Function)
214
+ value_or_function.to_code.call(**globals)
215
+ else
216
+ value_or_function.to_code
217
+ end
108
218
 
109
219
  String.new(CGI.escapeHTML(code_value.to_s))
110
220
  end
111
221
 
112
222
  def self.code_unescape(value_or_function = nil, **globals)
113
- if value_or_function.is_a?(Function)
114
- code_value = value_or_function.to_code.call(**globals)
115
- else
116
- code_value = value_or_function.to_code
117
- end
223
+ code_value =
224
+ if value_or_function.is_a?(Function)
225
+ value_or_function.to_code.call(**globals)
226
+ else
227
+ value_or_function.to_code
228
+ end
118
229
 
119
230
  String.new(Nokogiri::HTML.fragment(code_value.to_s).text)
120
231
  end
@@ -134,17 +245,19 @@ class Code
134
245
  return Html.new(fragment) unless code_contents.is_a?(List)
135
246
 
136
247
  code_contents.raw.each.with_index do |code_content, index|
137
- if code_content.is_an?(Html)
138
- content = Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
139
- else
140
- content = Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
141
- end
142
-
143
- if code_separator.is_an?(Html)
144
- separator = Nokogiri::HTML::DocumentFragment.parse(code_separator.to_html)
145
- else
146
- separator = Nokogiri::XML::Text.new(code_separator.to_s, fragment.document)
147
- end
248
+ content =
249
+ if code_content.is_an?(Html)
250
+ Nokogiri::HTML::DocumentFragment.parse(code_content.to_html)
251
+ else
252
+ Nokogiri::XML::Text.new(code_content.to_s, fragment.document)
253
+ end
254
+
255
+ separator =
256
+ if code_separator.is_an?(Html)
257
+ Nokogiri::HTML::DocumentFragment.parse(code_separator.to_html)
258
+ else
259
+ Nokogiri::XML::Text.new(code_separator.to_s, fragment.document)
260
+ end
148
261
 
149
262
  fragment.add_child(separator) unless index.zero?
150
263
  fragment.add_child(content)
@@ -154,24 +267,28 @@ class Code
154
267
  end
155
268
 
156
269
  def self.code_text(value_or_function = nil, **globals)
157
- if value_or_function.is_a?(Function)
158
- code_value = value_or_function.to_code.call(**globals)
159
- else
160
- code_value = value_or_function.to_code
161
- end
270
+ code_value =
271
+ if value_or_function.is_a?(Function)
272
+ value_or_function.to_code.call(**globals)
273
+ else
274
+ value_or_function.to_code
275
+ end
162
276
 
163
277
  fragment = Nokogiri::HTML::DocumentFragment.parse("")
164
- fragment.add_child(Nokogiri::XML::Text.new(code_value.to_s, fragment.document))
278
+ fragment.add_child(
279
+ Nokogiri::XML::Text.new(code_value.to_s, fragment.document)
280
+ )
165
281
 
166
282
  Html.new(fragment)
167
283
  end
168
284
 
169
285
  def self.code_raw(value_or_function = nil, **globals)
170
- if value_or_function.is_a?(Function)
171
- code_value = value_or_function.to_code.call(**globals)
172
- else
173
- code_value = value_or_function.to_code
174
- end
286
+ code_value =
287
+ if value_or_function.is_a?(Function)
288
+ value_or_function.to_code.call(**globals)
289
+ else
290
+ value_or_function.to_code
291
+ end
175
292
 
176
293
  if code_value.is_an?(Html)
177
294
  Html.new(Nokogiri::HTML::DocumentFragment.parse(code_value.to_html))
@@ -184,9 +184,12 @@ class Code
184
184
  http = ::Net::HTTP.new(uri.host, uri.port)
185
185
  http.use_ssl = true if uri.scheme == "https"
186
186
  default_timeout = timeout.nothing? ? DEFAULT_TIMEOUT : timeout.to_f
187
- open_timeout_value = open_timeout.nothing? ? default_timeout : open_timeout.to_f
188
- read_timeout_value = read_timeout.nothing? ? default_timeout : read_timeout.to_f
189
- write_timeout_value = write_timeout.nothing? ? default_timeout : write_timeout.to_f
187
+ open_timeout_value =
188
+ open_timeout.nothing? ? default_timeout : open_timeout.to_f
189
+ read_timeout_value =
190
+ read_timeout.nothing? ? default_timeout : read_timeout.to_f
191
+ write_timeout_value =
192
+ write_timeout.nothing? ? default_timeout : write_timeout.to_f
190
193
 
191
194
  http.open_timeout = open_timeout_value if open_timeout_value
192
195
  http.read_timeout = read_timeout_value if read_timeout_value
@@ -221,12 +224,9 @@ class Code
221
224
 
222
225
  begin
223
226
  response = http.request(request)
224
- rescue ::Timeout::Error,
225
- ::Net::OpenTimeout,
226
- ::Net::ReadTimeout,
227
- ::Errno::ETIMEDOUT
227
+ rescue ::Timeout::Error, ::Errno::ETIMEDOUT
228
228
  raise ::Code::Error, "http timeout"
229
- rescue OpenSSL::SSL::SSLError, IOError, EOFError
229
+ rescue OpenSSL::SSL::SSLError, IOError, SystemCallError, SocketError
230
230
  raise ::Code::Error, "http error"
231
231
  end
232
232
 
@@ -42,25 +42,29 @@ class Code
42
42
  end
43
43
 
44
44
  def self.serialize_event(event)
45
- EVENT_ATTRIBUTES.each_with_object({}) do |attribute, result|
46
- next unless event.respond_to?(attribute)
45
+ EVENT_ATTRIBUTES
46
+ .each_with_object({}) do |attribute, result|
47
+ next unless event.respond_to?(attribute)
47
48
 
48
- serialized = serialize_value(event.public_send(attribute))
49
- serialized =
50
- if attribute == :categories && serialized.is_a?(::Array)
51
- serialized.flatten(1)
52
- elsif scalar_event_attribute?(attribute) && serialized.is_a?(::Array)
53
- serialized.join(",")
54
- else
55
- serialized
56
- end
57
- result[attribute] = serialized unless serialized.nil?
58
- end.merge(
59
- timestamp: serialize_value(event.dtstamp),
60
- starts_at: serialize_value(event.dtstart),
61
- ends_at: serialize_value(event.dtend),
62
- all_day: !!serialize_date_like(event.dtstart).is_a?(::Date)
63
- ).compact
49
+ serialized = serialize_value(event.public_send(attribute))
50
+ serialized =
51
+ if attribute == :categories && serialized.is_a?(::Array)
52
+ serialized.flatten(1)
53
+ elsif scalar_event_attribute?(attribute) &&
54
+ serialized.is_a?(::Array)
55
+ serialized.join(",")
56
+ else
57
+ serialized
58
+ end
59
+ result[attribute] = serialized unless serialized.nil?
60
+ end
61
+ .merge(
62
+ timestamp: serialize_value(event.dtstamp),
63
+ starts_at: serialize_value(event.dtstart),
64
+ ends_at: serialize_value(event.dtend),
65
+ all_day: !!serialize_date_like(event.dtstart).is_a?(::Date)
66
+ )
67
+ .compact
64
68
  end
65
69
 
66
70
  def self.serialize_value(value)
@@ -60,7 +60,8 @@ class Code
60
60
  code_value
61
61
  else
62
62
  receiver.code_fetch(raw.last).call(
63
- **args.merge(context: args.fetch(:context, context)),
63
+ **args,
64
+ context: args.fetch(:context, context),
64
65
  operator: assignment_operator.chop,
65
66
  arguments: [code_value]
66
67
  )
@@ -88,15 +89,16 @@ class Code
88
89
  arguments: Object::List.new([code_value])
89
90
  )
90
91
  else
91
- next_value = receiver.call(
92
- **args,
93
- operator: raw.last,
94
- arguments: Object::List.new
95
- ).call(
96
- **args,
97
- operator: assignment_operator.chop,
98
- arguments: Object::List.new([code_value])
99
- )
92
+ next_value =
93
+ receiver.call(
94
+ **args,
95
+ operator: raw.last,
96
+ arguments: Object::List.new
97
+ ).call(
98
+ **args,
99
+ operator: assignment_operator.chop,
100
+ arguments: Object::List.new([code_value])
101
+ )
100
102
 
101
103
  receiver.call(
102
104
  **args,
@@ -1715,11 +1715,7 @@ class Code
1715
1715
  end
1716
1716
 
1717
1717
  if code_argument.is_a?(Class)
1718
- return List.new(
1719
- raw
1720
- .select { |code_element| code_element.is_a?(code_argument.raw) }
1721
- .uniq
1722
- )
1718
+ return List.new(raw.grep(code_argument.raw).uniq)
1723
1719
  end
1724
1720
 
1725
1721
  index = 0
@@ -10,6 +10,7 @@ class Code
10
10
  definition_context,
11
11
  explicit_arguments: false
12
12
  )
13
+ _explicit_arguments = explicit_arguments
13
14
  @parent = parent.to_code
14
15
  @forwarded_arguments = forwarded_arguments.to_code
15
16
  @code_self = code_self.to_code
@@ -23,7 +24,7 @@ class Code
23
24
  explicit_arguments = args.fetch(:explicit_arguments, false)
24
25
 
25
26
  @parent.code_call(
26
- *(arguments_for(code_arguments, explicit_arguments).raw),
27
+ *arguments_for(code_arguments, explicit_arguments).raw,
27
28
  explicit_arguments: explicit_arguments,
28
29
  bound_self: @code_self,
29
30
  **multi_fetch(args, *GLOBALS).merge(context: parent_context)
@@ -62,32 +62,29 @@ class Code
62
62
  def initialize(*args, **_kargs, &_block)
63
63
  ::Time.zone ||= DEFAULT_ZONE
64
64
 
65
- if args.first.is_a?(String) || args.first.is_a?(::String)
66
- self.raw = ::Time.zone.parse(args.first.to_s) || raise(Error)
67
- elsif args.first.is_a?(Time)
68
- self.raw = args.first.raw.dup.in_time_zone(::Time.zone)
69
- elsif args.first.is_a?(::Time)
70
- self.raw = args.first.dup.in_time_zone(::Time.zone)
71
- elsif args.first.is_a?(Date)
72
- self.raw = args.first.raw.to_time.in_time_zone(::Time.zone)
73
- elsif args.first.is_a?(::Date)
74
- self.raw = args.first.to_time.in_time_zone(::Time.zone)
75
- elsif args.first.is_a?(::ActiveSupport::TimeWithZone)
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)
88
- else
89
- self.raw = ::Time.zone.now
90
- end
65
+ first = args.first
66
+ self.raw =
67
+ case first
68
+ when String, ::String
69
+ ::Time.zone.parse(first.to_s) || raise(Error)
70
+ when Time
71
+ first.raw.dup.in_time_zone(::Time.zone)
72
+ when ::Time
73
+ first.dup.in_time_zone(::Time.zone)
74
+ when Date
75
+ first.raw.to_time.in_time_zone(::Time.zone)
76
+ when ::Date
77
+ first.to_time.in_time_zone(::Time.zone)
78
+ when ::ActiveSupport::TimeWithZone
79
+ first.dup
80
+ when Integer, Decimal, ::Integer, ::Float, ::BigDecimal
81
+ code_value = first.to_code
82
+ timestamp =
83
+ (code_value.is_a?(Decimal) ? code_value.raw.to_r : code_value.raw)
84
+ ::Time.zone.at(timestamp)
85
+ else
86
+ ::Time.zone.now
87
+ end
91
88
  end
92
89
 
93
90
  def self.call(**args)
@@ -831,10 +828,15 @@ class Code
831
828
  requested_locale = code_locale.raw&.to_s
832
829
  locale = requested_locale&.presence_in(LOCALES)&.to_sym
833
830
  locale ||= ::I18n.locale
834
- locale = ::I18n.locale unless ::I18n.available_locales.include?(locale.to_sym)
831
+ locale = ::I18n.locale unless ::I18n.available_locales.include?(
832
+ locale.to_sym
833
+ )
835
834
 
836
835
  format = code_format.raw || :default
837
- format = format.to_sym if ::I18n.exists?("time.formats.#{format}", locale)
836
+ format = format.to_sym if ::I18n.exists?(
837
+ "time.formats.#{format}",
838
+ locale
839
+ )
838
840
 
839
841
  String.new(::I18n.l(raw, format: format, locale: locale))
840
842
  end
@@ -900,14 +902,14 @@ class Code
900
902
  code_seconds.code_to_integer.raw + code_second.code_to_integer.raw
901
903
 
902
904
  code_change(
903
- year:,
904
- month:,
905
- day:,
906
- week_day:,
907
- week:,
908
- hour:,
909
- minute:,
910
- second:
905
+ year: year,
906
+ month: month,
907
+ day: day,
908
+ week_day: week_day,
909
+ week: week,
910
+ hour: hour,
911
+ minute: minute,
912
+ second: second
911
913
  )
912
914
  end
913
915
 
@@ -952,14 +954,14 @@ class Code
952
954
  code_seconds.code_to_integer.raw - code_second.code_to_integer.raw
953
955
 
954
956
  code_change(
955
- year:,
956
- month:,
957
- day:,
958
- week_day:,
959
- week:,
960
- hour:,
961
- minute:,
962
- second:
957
+ year: year,
958
+ month: month,
959
+ day: day,
960
+ week_day: week_day,
961
+ week: week,
962
+ hour: hour,
963
+ minute: minute,
964
+ second: second
963
965
  )
964
966
  end
965
967