no_regex 0.1.1 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +95 -69
- data/lib/no_regex/pattern_methods.rb +3 -2
- data/lib/no_regex/string_extensions.rb +1 -1
- data/lib/no_regex/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14eee48041a9af9e8eb4d1b7fc16ad89ea2655897e5171dd1f7061cb5a982a92
|
4
|
+
data.tar.gz: 45673b07974a3814c9d3db94fac6177ce8ca98f428e38de42dbd1e5ab3132034
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2697530e3cb7641968aae80d95026f5efc990ee327765697fb084517a3aa2de88facb9eb7139c40e6f68d5a0ac1321818337f75cabe7cc1bc95a626285d0a45
|
7
|
+
data.tar.gz: 620a663f1a22948421f04c3f975301e15f0cbc623065c1814fb38f1d003644c5f4acae264365f4ba5e5197b48c914495fc5ef67049be3992aa1a6647764c9bd1
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.2.0] - 2025-21-07
|
4
|
+
### Changed
|
5
|
+
- `is_number?` now accepts all numeric formats (integers and decimals, positive and negative)
|
6
|
+
- Previously only accepted positive integers
|
7
|
+
- Use `is_integer` for strictly whole numbers
|
8
|
+
- Added "Transform Your Code" section to README for better first impressions
|
data/README.md
CHANGED
@@ -1,7 +1,35 @@
|
|
1
|
-
|
1
|
+
# NoRegex
|
2
2
|
|
3
3
|
Write Ruby without regex! A gem that provides simple, readable methods to replace complex regular expressions for string validation and manipulation.
|
4
4
|
|
5
|
+
## Transform Your Code
|
6
|
+
|
7
|
+
**BEFORE** - From complex regex patterns:
|
8
|
+
```ruby
|
9
|
+
validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
|
10
|
+
validates :phone, format: { with: /\A\+?[\d\s\-\(\)]+\z/ }
|
11
|
+
|
12
|
+
if params[:search].match?(/\A\d+\z/)
|
13
|
+
# It's a number
|
14
|
+
end
|
15
|
+
|
16
|
+
user_input.gsub(/[`a-zA-Z0-9]/, '')
|
17
|
+
```
|
18
|
+
|
19
|
+
**AFTER** - To simple, readable methods!
|
20
|
+
```ruby
|
21
|
+
validates :email, format: { with: is_email? }
|
22
|
+
validates :phone, format: { with: is_phone_number? }
|
23
|
+
|
24
|
+
if params[:search].is_number?
|
25
|
+
# It's a number
|
26
|
+
end
|
27
|
+
|
28
|
+
user_input.remove_special_chars
|
29
|
+
```
|
30
|
+
|
31
|
+
No more googling regex patterns or debugging cryptic expressions!
|
32
|
+
|
5
33
|
## Installation
|
6
34
|
|
7
35
|
Add this line to your application's Gemfile:
|
@@ -22,6 +50,32 @@ Or install it yourself as:
|
|
22
50
|
$ gem install no_regex
|
23
51
|
```
|
24
52
|
|
53
|
+
## Rails Integration
|
54
|
+
|
55
|
+
To use the `format: { with: is_number? }` syntax in your Rails models or form objects, you need to extend your class with `NoRegex::PatternMethods`:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class User < ApplicationRecord
|
59
|
+
extend NoRegex::PatternMethods
|
60
|
+
|
61
|
+
validates :email, format: { with: is_email? }
|
62
|
+
validates :phone, format: { with: is_phone_number? }
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
For form objects:
|
67
|
+
```ruby
|
68
|
+
class MyFormObject
|
69
|
+
include ActiveModel::Model
|
70
|
+
extend NoRegex::PatternMethods
|
71
|
+
|
72
|
+
validates :search_term, format: { with: is_number? }
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
This makes all the pattern methods available to your validations. Once extended, you can use any of the validation methods in the Format Validations section.
|
77
|
+
|
78
|
+
|
25
79
|
## Usage
|
26
80
|
|
27
81
|
NoRegex extends Ruby's String, Integer, Float, and other classes with intuitive methods that eliminate the need for regex:
|
@@ -55,74 +109,12 @@ require 'no_regex'
|
|
55
109
|
3.14.to_percentage # => "314.0%"
|
56
110
|
```
|
57
111
|
|
58
|
-
## Rails Integration
|
59
|
-
|
60
|
-
### Using Format Validations in Rails
|
61
|
-
|
62
|
-
To use the `format: { with: is_number? }` syntax in your Rails models or form objects, you need to extend your class with `NoRegex::PatternMethods`:
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
class User < ApplicationRecord
|
66
|
-
extend NoRegex::PatternMethods
|
67
|
-
|
68
|
-
validates :email, format: { with: is_email? }
|
69
|
-
validates :phone, format: { with: is_phone_number? }
|
70
|
-
end
|
71
|
-
```
|
72
|
-
|
73
|
-
For form objects:
|
74
|
-
```ruby
|
75
|
-
class MyFormObject
|
76
|
-
include ActiveModel::Model
|
77
|
-
extend NoRegex::PatternMethods
|
78
|
-
|
79
|
-
validates :search_term, format: { with: is_number? }
|
80
|
-
end
|
81
|
-
```
|
82
|
-
|
83
|
-
This makes all the pattern methods available to your validations. Once extended, you can use any of the validation methods shown below.
|
84
|
-
|
85
|
-
### All Available Format Validators
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
validates :field, format: { with: is_number? } # Only digits
|
89
|
-
validates :field, format: { with: is_letters? } # Only letters
|
90
|
-
validates :field, format: { with: is_alphanumeric? } # Letters and numbers
|
91
|
-
validates :field, format: { with: is_email? } # Email format
|
92
|
-
validates :field, format: { with: is_phone_number? } # Phone format
|
93
|
-
validates :field, format: { with: is_url? } # URL format
|
94
|
-
validates :field, format: { with: is_zip_code? } # ZIP code
|
95
|
-
validates :field, format: { with: is_hex_color? } # Hex colors
|
96
|
-
validates :field, format: { with: is_username? } # Usernames
|
97
|
-
validates :field, format: { with: is_decimal? } # Decimals
|
98
|
-
validates :field, format: { with: is_integer? } # Integers
|
99
|
-
validates :field, format: { with: is_positive_number? } # Positive numbers
|
100
|
-
validates :field, format: { with: is_uuid? } # UUIDs
|
101
|
-
validates :field, format: { with: is_credit_card? } # Credit cards
|
102
|
-
validates :field, format: { with: is_ssn? } # SSN format
|
103
|
-
validates :field, format: { with: is_ipv4? } # IP addresses
|
104
|
-
validates :field, format: { with: is_time_24h? } # 24-hour time
|
105
|
-
validates :field, format: { with: is_date_yyyy_mm_dd? } # YYYY-MM-DD dates
|
106
|
-
```
|
107
|
-
|
108
|
-
### Custom Validators (Alternative Approach)
|
109
|
-
|
110
|
-
```ruby
|
111
|
-
class User < ApplicationRecord
|
112
|
-
validates :phone, number: true
|
113
|
-
validates :name, letters: { message: "can only contain letters" }
|
114
|
-
validates :username, alphanumeric: true
|
115
|
-
validates :email, email_format: true
|
116
|
-
validates :website, url_format: { allow_blank: true }
|
117
|
-
end
|
118
|
-
```
|
119
|
-
|
120
|
-
## Available Methods
|
121
|
-
|
122
112
|
### Validation Methods
|
123
113
|
|
124
|
-
- `is_number?` - Check if string contains
|
114
|
+
- `is_number?` - Check if string contains a number
|
125
115
|
- `is_letters?` - Check if string contains only letters
|
116
|
+
- `is_integer?` - Check if string is an integer
|
117
|
+
- `is_decimal?` - Check if string is a decimal number
|
126
118
|
- `is_alphanumeric?` - Check if string contains only letters and numbers
|
127
119
|
- `is_email?` - Check if string is a valid email format
|
128
120
|
- `is_blank?` - Check if string is nil or contains only whitespace
|
@@ -131,8 +123,6 @@ end
|
|
131
123
|
- `is_zip_code?` - Check if string is a valid ZIP code
|
132
124
|
- `is_hex_color?` - Check if string is a valid hex color
|
133
125
|
- `is_username?` - Check if string is a valid username (letters, numbers, _, -)
|
134
|
-
- `is_decimal?` - Check if string is a decimal number
|
135
|
-
- `is_integer?` - Check if string is an integer
|
136
126
|
- `is_positive_number?` - Check if string is a positive number
|
137
127
|
- `is_uuid?` - Check if string is a valid UUID
|
138
128
|
- `is_credit_card?` - Check if string is a valid credit card number
|
@@ -166,6 +156,42 @@ end
|
|
166
156
|
- `word_count` - Count words in string
|
167
157
|
- `truncate(length, ellipsis)` - Truncate string to specified length with ellipsis
|
168
158
|
|
159
|
+
|
160
|
+
### Format Validators
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
validates :field, format: { with: is_number? } # Only digits
|
164
|
+
validates :field, format: { with: is_letters? } # Only letters
|
165
|
+
validates :field, format: { with: is_integer? } # Integers
|
166
|
+
validates :field, format: { with: is_decimal? } # Decimals
|
167
|
+
validates :field, format: { with: is_alphanumeric? } # Letters and numbers
|
168
|
+
validates :field, format: { with: is_email? } # Email format
|
169
|
+
validates :field, format: { with: is_phone_number? } # Phone format
|
170
|
+
validates :field, format: { with: is_url? } # URL format
|
171
|
+
validates :field, format: { with: is_zip_code? } # ZIP code
|
172
|
+
validates :field, format: { with: is_hex_color? } # Hex colors
|
173
|
+
validates :field, format: { with: is_username? } # Usernames
|
174
|
+
validates :field, format: { with: is_positive_number? } # Positive numbers
|
175
|
+
validates :field, format: { with: is_uuid? } # UUIDs
|
176
|
+
validates :field, format: { with: is_credit_card? } # Credit cards
|
177
|
+
validates :field, format: { with: is_ssn? } # SSN format
|
178
|
+
validates :field, format: { with: is_ipv4? } # IP addresses
|
179
|
+
validates :field, format: { with: is_time_24h? } # 24-hour time
|
180
|
+
validates :field, format: { with: is_date_yyyy_mm_dd? } # YYYY-MM-DD dates
|
181
|
+
```
|
182
|
+
|
183
|
+
### Custom Validators (Alternative Approach)
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
class User < ApplicationRecord
|
187
|
+
validates :phone, number: true
|
188
|
+
validates :name, letters: { message: "can only contain letters" }
|
189
|
+
validates :username, alphanumeric: true
|
190
|
+
validates :email, email_format: true
|
191
|
+
validates :website, url_format: { allow_blank: true }
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
169
195
|
## Examples
|
170
196
|
|
171
197
|
```ruby
|
@@ -216,4 +242,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/Derity
|
|
216
242
|
|
217
243
|
## License
|
218
244
|
|
219
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
245
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -3,9 +3,10 @@
|
|
3
3
|
module NoRegex
|
4
4
|
module PatternMethods
|
5
5
|
# Define methods that return regex patterns
|
6
|
+
#
|
6
7
|
def is_number?
|
7
|
-
# Examples: "
|
8
|
-
/\A
|
8
|
+
# Examples: "123", "-456", "12.34", "-0.5", "0", "999.99"
|
9
|
+
/\A-?\d+(\.\d+)?\z/
|
9
10
|
end
|
10
11
|
|
11
12
|
def is_letters?
|
data/lib/no_regex/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: no_regex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Wright
|
@@ -59,6 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- CHANGELOG.md
|
62
63
|
- README.md
|
63
64
|
- lib/no_regex.rb
|
64
65
|
- lib/no_regex/aliases.rb
|