class SpicyValidation::Renderer
Attributes
table_name[R]
Public Class Methods
choose_table_name()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 30 def self.choose_table_name puts "\e[33m[warning] If you generate validation, model file will be overwritten.\e[0m" hash_tables = Schema.table_names.map.with_index { |table, index| [index.to_s.to_sym, table] }.to_h p hash_tables while true print "Type a number you wanna generate validation > " num = $stdin.gets.chomp.to_sym break if hash_tables.key?(num) p "Type a number correctly!" end hash_tables[num] end
generate(dry_run: false)
click to toggle source
# File lib/spicy_validation/renderer.rb, line 14 def self.generate(dry_run: false) table = choose_table_name object_table = new(table_name: table) return if object_table.validations.empty? object_table.write! if generate?(dry_run: dry_run) end
generate?(dry_run:)
click to toggle source
# File lib/spicy_validation/renderer.rb, line 26 def self.generate?(dry_run:) !dry_run end
new(table_name:)
click to toggle source
# File lib/spicy_validation/renderer.rb, line 10 def initialize(table_name:) @table_name = table_name end
Public Instance Methods
content()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 49 def content <<~MODEL class #{model_name} < ApplicationRecord #{validations.join("\n ")} end MODEL end
model_name()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 57 def model_name table_name.classify end
validations()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 45 def validations normal_validations + unique_validations end
write!()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 22 def write! File.write(model_path, content) end
Private Instance Methods
model_path()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 71 def model_path File.join(Rails.root.glob("app/models/**/#{model_name.underscore}.rb")) end
normal_validations()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 63 def normal_validations Validation.normal_validations(table_name: table_name) end
unique_validations()
click to toggle source
# File lib/spicy_validation/renderer.rb, line 67 def unique_validations Validation.unique_validations(table_name: table_name) end