Module: Wirb::SchemaBuilder

Defined in:
lib/wirb/schema_builder.rb

Class Method Summary collapse

Class Method Details

.load_schema_from_yaml(yaml_path = :classic) ⇒ Object

Loads a color schema from a yaml file

If first argument is a String: path to yaml file
If first argument is a Symbol: bundled schema

Raises:

  • (LoadError)


6
7
8
9
10
11
12
13
# File 'lib/wirb/schema_builder.rb', line 6

def self.load_schema_from_yaml(yaml_path = :classic)
  schema_name, schema_yaml = resolve_schema_yaml(yaml_path)
  raise LoadError, "Could not load the Wirb schema at: #{yaml_path}" unless schema_yaml.is_a?(Hash)

  schema = normalize_schema(schema_yaml)
  schema[:name] = schema_name.to_sym
  schema
end

.normalize_schema(schema_yaml) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wirb/schema_builder.rb', line 24

def self.normalize_schema(schema_yaml)
  normalized_schema = Hash[ schema_yaml.map{ |k,v| [k.to_s.to_sym, Array( v )] } ]
  %w(
    hash
    array
    set
    symbol_string
    string
    regexp
    rational
    complex
    object
  ).each{ |what|
    values = normalized_schema.delete(:"#{what}_delimiter")
    normalized_schema[:"open_#{what}"]  = values
    normalized_schema[:"close_#{what}"] = values
  }

  normalized_schema
end

.resolve_schema_yaml(yaml_path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/wirb/schema_builder.rb', line 15

def self.resolve_schema_yaml(yaml_path)
  if yaml_path.is_a? Symbol # bundled themes
    datadir = Gem.loaded_specs['wirb'].datadir
    [yaml_path.to_s, YAML.load_file(File.join(datadir, "#{yaml_path}.yml"))]
  else
    [File.basename(yaml_path).gsub(/\.yml$/, ''), YAML.load_file(yaml_path)]
  end
end