Class: Roro::Configurators::Validator

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/roro/configurators/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#adventure_name, #all_inflections, #build_paths, #children, #file_extension, #file_name, #read_yaml, #sanitize, #sentence_from, #sort_hash_deeply, #stack_is_adventure?, #stack_is_dotfile?, #stack_is_empty?, #stack_is_file?, #stack_is_ignored?, #stack_is_inflection?, #stack_is_inflection_stub?, #stack_is_itinerary_path?, #stack_is_nil?, #stack_is_node?, #stack_is_parent?, #stack_is_stack?, #stack_is_story?, #stack_is_story_path?, #stack_is_storyfile?, #stack_is_templates?, #stack_name, #stack_not_present?, #stack_parent, #stack_parent_path, #stack_stories, #stack_type, #stack_unpermitted?, #story_is_empty?, #story_name, #story_paths

Constructor Details

#initialize(stack = nil, structure = nil) ⇒ Validator

Returns a new instance of Validator.



11
12
13
14
15
16
17
18
19
# File 'lib/roro/configurators/validator.rb', line 11

def initialize(stack = nil, structure = nil)
  @stack         = stack     || Roro::CLI.stacks
  @structure     = structure || StructureBuilder.build
  @error         = Roro::CatalogError
  @permitted_story_extensions  = %w[yml yaml]
  @permitted_hidden_extensions = %w[.keep .gitkeep]
  @permitted_extensions        = @permitted_story_extensions +
                                 @permitted_hidden_extensions
end

Instance Attribute Details

#ext_permittedObject (readonly)

Returns the value of attribute ext_permitted.



9
10
11
# File 'lib/roro/configurators/validator.rb', line 9

def ext_permitted
  @ext_permitted
end

#permitted_hidden_extensionsObject (readonly)

Returns the value of attribute permitted_hidden_extensions.



9
10
11
# File 'lib/roro/configurators/validator.rb', line 9

def permitted_hidden_extensions
  @permitted_hidden_extensions
end

#permitted_story_extensionsObject (readonly)

Returns the value of attribute permitted_story_extensions.



9
10
11
# File 'lib/roro/configurators/validator.rb', line 9

def permitted_story_extensions
  @permitted_story_extensions
end

#stackObject (readonly)

Returns the value of attribute stack.



9
10
11
# File 'lib/roro/configurators/validator.rb', line 9

def stack
  @stack
end

#structureObject (readonly)

Returns the value of attribute structure.



9
10
11
# File 'lib/roro/configurators/validator.rb', line 9

def structure
  @structure
end

Instance Method Details

#base_validate(s) ⇒ Object

TODO: validate ambiguous Add validation to ensure story directory has either a) a matching storyfile or b) at least one child stack, story, or inflection.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/roro/configurators/validator.rb', line 25

def base_validate(s)
  case
  when stack_is_nil?(s)
    @msg = 'Catalog not present'
  when stack_is_ignored?(s)
    return
  when stack_is_empty?(s)
    @msg = 'Catalog cannot be an empty folder'
  when stack_unpermitted?(s)
    @msg = 'Catalog has unpermitted extension'
  when stack_unrecognized?(s)
    @msg = 'Catalog must be a story, a stack, or an inflection'
  end
end

#stack_unrecognized?(stack) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/roro/configurators/validator.rb', line 40

def stack_unrecognized?(stack)
  stack_type(stack).nil?
end

#status(plot, structure) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/roro/configurators/validator.rb', line 81

def status(plot, structure)
  case
  when !plot
    :plot_empty
  when !plot.is_a?(structure.class)
    :unexpected_class
  when plot.is_a?(Array) && plot.any?(nil)
    :array_empty
  when plot.is_a?(Hash) && story_has_unpermitted_keys?(plot, structure)
    :unpermitted_keys
  when plot.is_a?(Hash) && story_has_nil_value?(plot)
    :hash_value_nil
  when plot.is_a?(Hash) && structure.keys&.any?
    :hash_expecting_values
  when plot.is_a?(Array)
    :array_valid
  end
end

#story_has_nil_value?(content) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/roro/configurators/validator.rb', line 106

def story_has_nil_value?(content)
  content&.values&.include?(nil)
end

#story_has_unpermitted_keys?(content, structure) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/roro/configurators/validator.rb', line 100

def story_has_unpermitted_keys?(content, structure)
  @permitted = structure&.keys
  @unpermitted = content.keys - @permitted
  @permitted.any? && @unpermitted.any?
end

#validate(stack = nil) ⇒ Object

Raises:

  • (@error)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/roro/configurators/validator.rb', line 44

def validate(stack = nil)
  @msg = nil
  stack ||= @stack
  @stack_root ||= stack
  base_validate(stack)
  case stack_type(stack)
  when :storyfile
    validate_plot(read_yaml(stack))
  when :story
    children(stack).each { |c| validate(c) }
  when :inflection
    children(stack).each { |c| validate(c) }
  when :stack
    children(stack).each { |c| validate(c) }
  end
  raise(@error, "#{@msg} in #{stack}") unless @msg.nil?
end

#validate_plot(plot, structure = @structure) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/roro/configurators/validator.rb', line 62

def validate_plot(plot, structure = @structure)
  case status(plot, structure)
  when :plot_empty
    @msg = 'Story file is empty'
  when :unexpected_class
    @msg = "'#{plot}' must be an instance of #{structure.class}"
  when :array_empty
    @msg = 'Story contains an empty array'
  when :unpermitted_keys
    @msg = "#{@unpermitted} not in permitted keys: #{@permitted}"
  when :hash_value_nil
    @msg = "Value for :#{plot.key(nil)} must not be nil"
  when :array_valid
    plot.each { |item| validate_plot(item, structure[0]) }
  when :hash_expecting_values
    plot.each { |k, v| validate_plot(v, structure[k]) }
  end
end