Module: Mokuji::Validators

Included in:
Converter, Exporter, Scanner
Defined in:
lib/mokuji/validators.rb

Overview

{ VALIDATORS }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate_options(hash) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/mokuji/validators.rb', line 6

def self.validate_options hash
  if hash['output_type']
    unless ['json', 'html', 'plain_html', 'plain_text'].include? hash['output_type']
      raise "Sorry, but, this output type is unknown."
    end
  end
end

Instance Method Details

#json_valid?(str) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/mokuji/validators.rb', line 58

def json_valid? str
  begin
    JSON.parse str
    return true
  rescue JSON::ParserError
    return false
  end
end

#validate_configurationObject



14
15
16
17
18
19
20
21
22
# File 'lib/mokuji/validators.rb', line 14

def validate_configuration
  unless Mokuji.method_defined? :configuration
    begin
      require 'mokuji'
    rescue LoadError
      raise "What have you done with this gem? 'mokuji.rb' is missing!"
    end
  end
end

#validate_data_from_converter(string) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/mokuji/validators.rb', line 49

def validate_data_from_converter string
  case
  when string.class ==! String
    raise "Wait a minute, this isn't a string!"
  when string.length === 0
    raise "The length of this string is zero. Lame!"
  end
end

#validate_data_from_scanner(hash) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/mokuji/validators.rb', line 40

def validate_data_from_scanner hash
  case
  when hash.class ==! Hash
    raise "Wait a minute, this isn't a hash!"
  when hash.empty?
    raise "This hash is empty. Lame!"
  end
end

#validate_export_path(path) ⇒ Object



33
34
35
36
37
38
# File 'lib/mokuji/validators.rb', line 33

def validate_export_path path
  case
  when !File.directory?(path)
    raise "Arr, that directory you want to export to, it doesn't quite exist."
  end
end

#validate_import_path(path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/mokuji/validators.rb', line 24

def validate_import_path path
  case
  when !File.directory?(path)
    raise "Arr, that directory you want to scan, it doesn't quite exist."
  when Dir.entries(path).slice(2..-1).empty?
    raise "Arr, that directory you want to scan, it's empty."
  end
end