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
Class Method Summary collapse
Instance Method Summary collapse
- #call(**args) ⇒ Object
- #code_extended? ⇒ Boolean
- #code_fixed_encoding? ⇒ Boolean
- #code_ignore_case? ⇒ Boolean
- #code_inspect ⇒ Object
- #code_match?(string) ⇒ Boolean
- #code_multiple_lines? ⇒ Boolean
- #code_no_encoding? ⇒ Boolean
- #code_options ⇒ Object
- #code_source ⇒ Object
- #code_to_string ⇒ Object
-
#initialize(*args, **kargs, &_block) ⇒ Regex
constructor
A new instance of Regex.
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 = 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. | ) elsif pattern.is_a?(::Regexp) ::Regexp.new(pattern.source, pattern. | ) else ::Regexp.new(pattern.to_s, ) end rescue ::RegexpError => e raise Error, "invalid regex: #{e.}" 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) 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. & ::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. & ::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. & ::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. & ::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. & ::Regexp::NOENCODING) == ::Regexp::NOENCODING) end |
#code_options ⇒ Object
196 197 198 199 200 |
# File 'lib/code/object/regex.rb', line 196 def Dictionary.new( OPTION_FLAGS.transform_values { |flag| (raw. & 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 |