Class: Snp::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/snp/data.rb

Overview

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 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, path = Path.new) ⇒ Data

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



26
27
28
29
# File 'lib/snp/data.rb', line 26

def initialize(template, path = Path.new)
  @template = template
  @path     = path
end

Class Method Details

.for(template) ⇒ Object

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.



21
22
23
# File 'lib/snp/data.rb', line 21

def self.for(template)
  new(template).to_hash
end

Instance Method Details

#to_hashObject

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

Returns a hash of the parsed data.



34
35
36
37
38
39
40
# File 'lib/snp/data.rb', line 34

def to_hash
  if absolute_path
    YAML.load_file(absolute_path)
  else
    {}
  end
end