Class: PrettyValidation::Renderer
- Inherits:
-
Object
- Object
- PrettyValidation::Renderer
- Defined in:
- lib/pretty_validation/renderer.rb
Instance Attribute Summary collapse
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #file_name ⇒ Object
- #file_path ⇒ Object
-
#initialize(table_name) ⇒ Renderer
constructor
A new instance of Renderer.
- #messages ⇒ Object
- #module_name ⇒ Object
- #render ⇒ Object
- #validations ⇒ Object
- #write! ⇒ Object
Constructor Details
#initialize(table_name) ⇒ Renderer
Returns a new instance of Renderer.
30 31 32 |
# File 'lib/pretty_validation/renderer.rb', line 30 def initialize(table_name) @table_name = table_name end |
Instance Attribute Details
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
7 8 9 |
# File 'lib/pretty_validation/renderer.rb', line 7 def table_name @table_name end |
Class Method Details
.generate(dry_run: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pretty_validation/renderer.rb', line 13 def self.generate(dry_run: false) if generate?(dry_run) && !File.directory?(validations_path) FileUtils.mkdir_p validations_path end Schema.table_names.each do |t| r = new t next if r.validations.empty? r.write! if generate?(dry_run) end end |
.generate?(dry_run) ⇒ Boolean
26 27 28 |
# File 'lib/pretty_validation/renderer.rb', line 26 def self.generate?(dry_run) PrettyValidation.config.auto_generate && !dry_run end |
.validations_path ⇒ Object
9 10 11 |
# File 'lib/pretty_validation/renderer.rb', line 9 def self.validations_path File.join('app', 'validations') end |
Instance Method Details
#file_name ⇒ Object
50 51 52 |
# File 'lib/pretty_validation/renderer.rb', line 50 def file_name "#{module_name.underscore}.rb" end |
#file_path ⇒ Object
54 55 56 |
# File 'lib/pretty_validation/renderer.rb', line 54 def file_path File.join(Renderer.validations_path, file_name) end |
#messages ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/pretty_validation/renderer.rb', line 66 def [ announce('generating'), render, announce('generated'), "\n" ].join end |
#module_name ⇒ Object
46 47 48 |
# File 'lib/pretty_validation/renderer.rb', line 46 def module_name "#{table_name.classify}Validation" end |
#render ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pretty_validation/renderer.rb', line 34 def render @render ||= <<-EOF module #{module_name} extend ActiveSupport::Concern included do #{validations.join("\n ")} end end EOF end |
#validations ⇒ Object
62 63 64 |
# File 'lib/pretty_validation/renderer.rb', line 62 def validations sexy_validations + uniq_validations end |
#write! ⇒ Object
58 59 60 |
# File 'lib/pretty_validation/renderer.rb', line 58 def write! File.write(file_path, render) end |