Class: YamlBot::KeyBot
- Inherits:
-
Object
- Object
- YamlBot::KeyBot
- Defined in:
- lib/yaml_bot/key_bot.rb
Constant Summary collapse
- TYPE_MAP =
{ object: Hash, list: Array, string: String, number: [Integer, Float], boolean: [TrueClass, FalseClass] }.freeze
- VALIDATION_CHECKS =
[ :check_if_key_is_required, :check_and_requires, :check_or_requires, :check_val_whitelist, :check_val_blacklist, :check_types ].freeze
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Returns the value of attribute defaults.
-
#invalid ⇒ Object
Returns the value of attribute invalid.
-
#key ⇒ Object
Returns the value of attribute key.
-
#yaml_file ⇒ Object
Returns the value of attribute yaml_file.
Instance Method Summary collapse
- #check_and_requires ⇒ Object
- #check_if_key_is_required ⇒ Object
-
#check_or_requires ⇒ Object
If the key being checked, or any of the keys in the “or_requires” list are in the yaml then check passes.
- #check_types ⇒ Object
- #check_val_blacklist ⇒ Object
- #check_val_whitelist ⇒ Object
-
#initialize(key, yaml_file, defaults) ⇒ KeyBot
constructor
A new instance of KeyBot.
- #validate ⇒ Object
Constructor Details
#initialize(key, yaml_file, defaults) ⇒ KeyBot
Returns a new instance of KeyBot.
24 25 26 27 28 29 |
# File 'lib/yaml_bot/key_bot.rb', line 24 def initialize(key, yaml_file, defaults) @defaults = defaults || {} @invalid = false @key = @defaults.merge(key) @yaml_file = yaml_file end |
Instance Attribute Details
#defaults ⇒ Object
Returns the value of attribute defaults.
5 6 7 |
# File 'lib/yaml_bot/key_bot.rb', line 5 def defaults @defaults end |
#invalid ⇒ Object
Returns the value of attribute invalid.
5 6 7 |
# File 'lib/yaml_bot/key_bot.rb', line 5 def invalid @invalid end |
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/yaml_bot/key_bot.rb', line 5 def key @key end |
#yaml_file ⇒ Object
Returns the value of attribute yaml_file.
5 6 7 |
# File 'lib/yaml_bot/key_bot.rb', line 5 def yaml_file @yaml_file end |
Instance Method Details
#check_and_requires ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/yaml_bot/key_bot.rb', line 47 def check_and_requires yaml_key = ParseBot.get_object_value(yaml_file, key['key']) return if yaml_key.nil? || key['and_requires'].nil? key['and_requires'].each do |k| next unless ParseBot.get_object_value(yaml_file, k).nil? @invalid = true puts "VIOLATION: Key #{key['key']} requires that the following key(s)" puts "also be defined: #{key['and_requires']}" break end end |
#check_if_key_is_required ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/yaml_bot/key_bot.rb', line 39 def check_if_key_is_required yaml_key = ParseBot.get_object_value(yaml_file, key['key']) return unless key['required_key'] && yaml_key.nil? return unless key['or_requires'].nil? @invalid = true puts "VIOLATION: Missing required key: #{key['key']}" end |
#check_or_requires ⇒ Object
If the key being checked, or any of the keys in the “or_requires” list are in the yaml then check passes
62 63 64 65 66 67 68 69 70 |
# File 'lib/yaml_bot/key_bot.rb', line 62 def check_or_requires return if key['or_requires'].nil? alt_key = search_for_alternate_key(key) value = ParseBot.get_object_value(yaml_file, key['key']) @invalid = value.nil? && alt_key.nil? return unless @invalid puts "VIOLATION: Key #{key['key']} or the following key(s)" puts "must be defined: #{key['and_requires']}" end |
#check_types ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/yaml_bot/key_bot.rb', line 88 def check_types return if key['types'].nil? value = ParseBot.get_object_value(yaml_file, key['key']) return if value.nil? @invalid = !value_is_a_valid_type?(key['types'], value) return unless @invalid puts "VIOLATION: Invalid value type specified for key: #{key['key']}" puts "#{value.class} given, valid types include #{key['types']}\n" end |
#check_val_blacklist ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/yaml_bot/key_bot.rb', line 80 def check_val_blacklist return if key['value_blacklist'].nil? value = ParseBot.get_object_value(yaml_file, key['key']) return unless key['value_blacklist'].include?(value) @invalid = true puts "VIOLATION: Blacklisted value specified #{value}\n" end |
#check_val_whitelist ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/yaml_bot/key_bot.rb', line 72 def check_val_whitelist return if key['value_whitelist'].nil? value = ParseBot.get_object_value(yaml_file, key['key']) return if key['value_whitelist'].include?(value) @invalid = true puts "VIOLATION: Invalid value #{value} for whitelist #{key['value_whitelist']}\n" end |
#validate ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/yaml_bot/key_bot.rb', line 31 def validate VALIDATION_CHECKS.each do |method| send(method) break if @invalid end @invalid ? 1 : 0 end |