Skip to content

Commit d48ed0f

Browse files
committed
Accept only valid snake case in interactive input
1 parent 1901b58 commit d48ed0f

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/cli.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,19 @@ def gets_number
139139
end
140140
end
141141

142+
def gets_snake_case
143+
loop do
144+
print '> '
145+
val = $stdin.gets.chomp
146+
return val if val.lower_snake_case?
147+
148+
puts 'Value has to be lower_snake_caase. Please try again.'
149+
end
150+
end
151+
142152
def fetch_args_interactive
143153
puts 'Choose your controller name (use lower_snake_case)'
144-
@args[:name] = gets_non_empty
154+
@args[:name] = gets_snake_case
145155

146156
puts 'Choose your parent controller'
147157
# passing parent as a string if we already have an object is hacky

lib/strings.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def to_camel
1616
self
1717
end
1818

19+
def lower_snake_case?
20+
match?(/^[a-z_]+$/)
21+
end
22+
1923
def to_snake
2024
return self if match?(/_/)
2125

spec/strings_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@
2020
expect('hello_world'.to_camel).to eq('HelloWorld')
2121
expect('hello'.to_camel).to eq('Hello')
2222
end
23+
it 'Should detect snake case' do
24+
expect('HelloWorld'.lower_snake_case?).to eq(false)
25+
expect('helloW'.lower_snake_case?).to eq(false)
26+
expect('HellO__'.lower_snake_case?).to eq(false)
27+
expect('hello'.lower_snake_case?).to eq(true)
28+
expect('hello_world'.lower_snake_case?).to eq(true)
29+
end
2330
end
2431
end

0 commit comments

Comments
 (0)