Class: Slinky::ConfigReader

Inherits:
Object
  • Object
show all
Defined in:
lib/slinky/config_reader.rb

Defined Under Namespace

Classes: ConfigEntry

Constant Summary collapse

HASH_TYPE =
"hash"
ARRAY_TYPE =
"array"
STRING_TYPE =
"string"
NUMBER_TYPE =
"number"
BOOL_TYPE =
"bool"
ANY_TYPE =
"any"
DEFAULT_SCRIPT_PRODUCT =
"/scripts.js"
DEFAULT_STYLE_PRODUCT =
"/styles.css"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_or_hash) ⇒ ConfigReader

Returns a new instance of ConfigReader.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/slinky/config_reader.rb', line 76

def initialize string_or_hash
  case string_or_hash
  when String
    @config = YAML::load(string_or_hash)
  when Hash
    @config = string_or_hash
  else
    raise TypeError.new("Config must be either a string or a hash")
  end
  ConfigReader.validate(@config)
end

Class Method Details

.emptyObject



56
57
58
# File 'lib/slinky/config_reader.rb', line 56

def self.empty
  new "{}"
end

.from_file(path) ⇒ Object



52
53
54
# File 'lib/slinky/config_reader.rb', line 52

def self.from_file path
  new File.open(path).read
end

.validate(config) ⇒ Object

Validates whether a supplied hash is well-formed according to the allowed entries



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slinky/config_reader.rb', line 62

def self.validate config
  entries = {}
  @entries.each{|e| entries[e.name] = e}
  errors = config.map{|k, v|
    if !entries[k]
      " * '#{k}' is not an allowed configuration key"
    end
  }.compact

  if !errors.empty?
    raise InvalidConfigError.new(errors.join("\n"))
  end
end

Instance Method Details

#[](x) ⇒ Object



97
98
99
# File 'lib/slinky/config_reader.rb', line 97

def [](x)
  @config[x]
end

#pushstate_for_path(path) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/slinky/config_reader.rb', line 88

def pushstate_for_path path
  if pushstate && pushstate.is_a?(Hash)
    p = pushstate.sort_by{|from, to| -from.count("/")}.find{|a|
      path.start_with? a[0]
    }
    p[1] if p
  end
end

#to_sObject



101
102
103
# File 'lib/slinky/config_reader.rb', line 101

def to_s
  @config.to_s
end