class CssValidator
Constants
- VENDOR_DIR
Public Instance Methods
validate_each(record, attribute, value)
click to toggle source
# File lib/css_validator.rb, line 8 def validate_each(record, attribute, value) node_path = `which node`.rstrip raise 'You do not have a Node.js installed, but it is required.' unless node_path && !node_path.empty? return if options[:allow_nil] && value.nil? return if options[:allow_blank] && value.blank? css_file = Tempfile.new(['css', '.css']) css_file.write(value) css_file.close cmd = "#{node_path} #{VENDOR_DIR}/cli.js #{css_file.path}" Open3.popen3(cmd) do |stdin, stdout, stderr| result = stdout.read if result =~ /error/ record.errors.add attribute, (options[:message] || 'is invalid') end end ensure css_file.unlink if css_file # deletes the temp file end