Class: Code::Object::Regex
Constant Summary
collapse
- CLASS_DOCUMENTATION =
{
name: "Regex",
description:
"represents a regular expression pattern with explicit matching options.",
examples: [
"Regex",
"Regex.new(\"a|b|c\")",
"Regex.new(\"^hello\", ignore_case: true)"
]
}.freeze
- INSTANCE_FUNCTIONS =
{
"match?" => {
name: "match?",
description: "returns whether the regex matches a string.",
examples: [
"Regex.new(\"a|b|c\").match?(\"b\")",
"Regex.new(\"^hello\").match?(\"hello world\")",
"Regex.new(\"^hello\").match?(\"say hello\")"
]
},
"source" => {
name: "source",
description: "returns the regex pattern source.",
examples: [
"Regex.new(\"a|b|c\").source",
"Regex.new(\"^hello\").source",
"Regex.new(\"hello\", ignore_case: true).source"
]
},
"options" => {
name: "options",
description: "returns the enabled regex options.",
examples: [
"Regex.new(\"hello\").options",
"Regex.new(\"hello\", ignore_case: true).options.ignore_case",
"Regex.new(\"hello\", multiple_lines: true).options.multiple_lines"
]
},
"ignore_case?" => {
name: "ignore_case?",
description: "returns whether the regex ignores case.",
examples: [
"Regex.new(\"hello\").ignore_case?",
"Regex.new(\"hello\", ignore_case: true).ignore_case?",
"Regex.new(\"hello\", ignore_case: false).ignore_case?"
]
},
"extended?" => {
name: "extended?",
description: "returns whether the regex ignores whitespace and comments.",
examples: [
"Regex.new(\"hello\").extended?",
"Regex.new(\"hello\", extended: true).extended?",
"Regex.new(\"hello\", extended: false).extended?"
]
},
"multiple_lines?" => {
name: "multiple_lines?",
description: "returns whether the regex matches across multiple lines.",
examples: [
"Regex.new(\"hello\").multiple_lines?",
"Regex.new(\"hello\", multiple_lines: true).multiple_lines?",
"Regex.new(\"hello\", multiple_lines: false).multiple_lines?"
]
},
"fixed_encoding?" => {
name: "fixed_encoding?",
description: "returns whether the regex has fixed encoding.",
examples: [
"Regex.new(\"hello\").fixed_encoding?",
"Regex.new(\"hello\", fixed_encoding: true).fixed_encoding?",
"Regex.new(\"hello\", fixed_encoding: false).fixed_encoding?"
]
},
"no_encoding?" => {
name: "no_encoding?",
description: "returns whether the regex has no encoding.",
examples: [
"Regex.new(\"hello\").no_encoding?",
"Regex.new(\"hello\", no_encoding: true).no_encoding?",
"Regex.new(\"hello\", no_encoding: false).no_encoding?"
]
}
}.freeze
- OPTION_FLAGS =
{
"ignore_case" => ::Regexp::IGNORECASE,
"extended" => ::Regexp::EXTENDED,
"multiple_lines" => ::Regexp::MULTILINE,
"fixed_encoding" => ::Regexp::FIXEDENCODING,
"no_encoding" => ::Regexp::NOENCODING
}.freeze
Constants inherited
from Code::Object
CLASS_FUNCTIONS, NUMBER_CLASSES
Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS
Instance Attribute Summary
#functions, #raw
Class Method Summary
collapse
Instance Method Summary
collapse
class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, instance_functions, maybe, #name, repeat, sorted_dictionary, |
#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?
Constructor Details
#initialize(*args, **kargs, &_block) ⇒ Regex
Returns a new instance of Regex.
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/code/object/regex.rb', line 104
def initialize(*args, **kargs, &_block)
raise Error, "Regex.new: pattern is required" if args.empty?
raise Error, "Regex.new: expected 1-2 arguments" if args.size > 2
pattern = args.first
option_values = {}
if args[1]
case args[1]
when Dictionary
args[1].raw.each do |key, value|
option_values[key.to_s] =
value.is_an?(Object) ? value.truthy? : !!value
end
when ::Hash
args[1].each do |key, value|
option_values[key.to_s] =
value.is_an?(Object) ? value.truthy? : !!value
end
else
raise Error, "Regex.new: options must be a dictionary"
end
end
kargs.each do |key, value|
option_values[key.to_s] =
value.is_an?(Object) ? value.truthy? : !!value
end
options =
option_values.reduce(0) do |flags, (name, enabled)|
flag = OPTION_FLAGS[name]
raise Error, "unknown regex option: #{name}" unless flag
enabled ? flags | flag : flags
end
self.raw =
if pattern.is_a?(Regex)
::Regexp.new(pattern.raw.source, pattern.raw.options | options)
elsif pattern.is_a?(::Regexp)
::Regexp.new(pattern.source, pattern.options | options)
else
::Regexp.new(pattern.to_s, options)
end
rescue ::RegexpError => e
raise Error, "invalid regex: #{e.message}"
end
|
Class Method Details
.function_documentation(scope) ⇒ Object
98
99
100
101
102
|
# File 'lib/code/object/regex.rb', line 98
def self.function_documentation(scope)
return INSTANCE_FUNCTIONS if scope == :instance
{}
end
|
Instance Method Details
#call(**args) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/code/object/regex.rb', line 153
def call(**args)
code_operator = args.fetch(:operator, nil).to_code
code_arguments = args.fetch(:arguments, List.new).to_code
code_value = code_arguments.code_first
case code_operator.to_s
when "match?"
sig(args) { String }
code_match?(code_value)
when "source"
sig(args)
code_source
when "options"
sig(args)
code_options
when "ignore_case?"
sig(args)
code_ignore_case?
when "extended?"
sig(args)
code_extended?
when "multiple_lines?"
sig(args)
code_multiple_lines?
when "fixed_encoding?"
sig(args)
code_fixed_encoding?
when "no_encoding?"
sig(args)
code_no_encoding?
else
super
end
end
|
#code_extended? ⇒ Boolean
206
207
208
|
# File 'lib/code/object/regex.rb', line 206
def code_extended?
Boolean.new((raw.options & ::Regexp::EXTENDED) == ::Regexp::EXTENDED)
end
|
#code_fixed_encoding? ⇒ Boolean
214
215
216
217
218
|
# File 'lib/code/object/regex.rb', line 214
def code_fixed_encoding?
Boolean.new(
(raw.options & ::Regexp::FIXEDENCODING) == ::Regexp::FIXEDENCODING
)
end
|
#code_ignore_case? ⇒ Boolean
202
203
204
|
# File 'lib/code/object/regex.rb', line 202
def code_ignore_case?
Boolean.new((raw.options & ::Regexp::IGNORECASE) == ::Regexp::IGNORECASE)
end
|
#code_inspect ⇒ Object
228
229
230
|
# File 'lib/code/object/regex.rb', line 228
def code_inspect
String.new(raw.inspect)
end
|
#code_match?(string) ⇒ Boolean
188
189
190
|
# File 'lib/code/object/regex.rb', line 188
def code_match?(string)
Boolean.new(raw.match?(string.to_s))
end
|
#code_multiple_lines? ⇒ Boolean
210
211
212
|
# File 'lib/code/object/regex.rb', line 210
def code_multiple_lines?
Boolean.new((raw.options & ::Regexp::MULTILINE) == ::Regexp::MULTILINE)
end
|
#code_no_encoding? ⇒ Boolean
220
221
222
|
# File 'lib/code/object/regex.rb', line 220
def code_no_encoding?
Boolean.new((raw.options & ::Regexp::NOENCODING) == ::Regexp::NOENCODING)
end
|
#code_options ⇒ Object
196
197
198
199
200
|
# File 'lib/code/object/regex.rb', line 196
def code_options
Dictionary.new(
OPTION_FLAGS.transform_values { |flag| (raw.options & flag) == flag }
)
end
|
#code_source ⇒ Object
192
193
194
|
# File 'lib/code/object/regex.rb', line 192
def code_source
String.new(raw.source)
end
|
#code_to_string ⇒ Object
224
225
226
|
# File 'lib/code/object/regex.rb', line 224
def code_to_string
code_source
end
|