Module: Sinatra::Validation::Extension

Defined in:
lib/herbert/Services.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Um, dragons… Fucking swarm… But I’ll try to explain this anyway. We’ll scan the defined settings.validation dir for dirs. Those found dirs will denote <resource>s. Then, we will scan the “resource” dirs for files. These files will represent one http <verb>.yaml each. And then, we create hierchy of validation schemas following this pattern: ::setting.validation::<resource>::<verb_schema> where the <verb_schema> equals <verb>.capitalize and contains parsed contents of <verb>.yaml file. Please note that I haven’t used a single (.*_)eval even though I was terribly tempted. And I also documented this method. I’m so awesome, considerate and drunk, am I not? Uh, yea, and notice the nice cascade of ‘end’s on the end



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/herbert/Services.rb', line 46

def self.registered(app)
  # Define the ::<schema_root> module
  validation_module = Kernel.const_set(app.settings.validation[:module], Module.new)
  schema_root = Dir.new(File.join(app.settings.root, app.settings.validation[:path]))
  log.h_debug("Loading validation schemas from #{schema_root.path}");
  # For each resource
  schema_root.each do |resource_dir|
    next if %w{.. .}.include? resource_dir
    resource_name = resource_dir
    resource_dir = File.join(schema_root, resource_dir)
    # Create <schema_root>::<resource> module
    validation_module.const_set(resource_name, Module.new {})
    if File.directory?(resource_dir) then
      Dir.new(resource_dir).each do |verb|
        next if %w{.. .}.include? verb
        # And create the <schema_root>::<resource>::<verb_schema> constant
        validation_module.const_get(resource_name).const_set(/^(\w+)(\.yaml|\.yml)/.match(verb)[1].capitalize, ::YAML.load_file(File.join(resource_dir, verb)))
      end
    end
  end
end