class Snp::Data

Snp::Data

This class is responsible for fetching the default data to be used when compiling a template. This defaults to the data available on a YAML file located in `SNP_PATH`, if available.

Example

# Given there is a jquery.yml file in `SNP_PATH`, then
data = Snp::Data.for('jquery')
# => { 'version' => '1.9', 'cdn' => true }

Public Class Methods

for(template) click to toggle source

Public: fetches the default data for the given template and parses it.

template - the template name whose data is to be fetched.

Returns a hash with the data.

# File lib/snp/data.rb, line 21
def self.for(template)
  new(template).to_hash
end
new(template, path = Path.new) click to toggle source

Public: creates a new `Snp::Data` instance for the template given.

# File lib/snp/data.rb, line 26
def initialize(template, path = Path.new)
  @template = template
  @path     = path
end

Public Instance Methods

to_hash() click to toggle source

Public: fetches the data file and parses it, if available.

Returns a hash of the parsed data.

# File lib/snp/data.rb, line 34
def to_hash
  if absolute_path
    YAML.load_file(absolute_path)
  else
    {}
  end
end

Private Instance Methods

absolute_path() click to toggle source

Internal: returns the absolute path to the template file, searching for the directories in `SNP_PATH`.

# File lib/snp/data.rb, line 46
def absolute_path
  @path.which(@template, 'yml')
end