Class: Soundwave::Page

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, path) ⇒ Page

Returns a new instance of Page.



70
71
72
73
# File 'lib/soundwave.rb', line 70

def initialize(site, path)
  @site = site
  @path = Pathname(path).expand_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



68
69
70
# File 'lib/soundwave.rb', line 68

def path
  @path
end

#siteObject (readonly)

Returns the value of attribute site.



68
69
70
# File 'lib/soundwave.rb', line 68

def site
  @site
end

Instance Method Details

#base_pathObject



83
84
85
# File 'lib/soundwave.rb', line 83

def base_path
  relative_path.sub(".mustache",'')
end

#output_pathObject



79
80
81
# File 'lib/soundwave.rb', line 79

def output_path
  relative_path.sub('.mustache','.html')
end

#read_dataObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/soundwave.rb', line 97

def read_data
  if data_file = site.data_trail.find(base_path)
    case File.extname(data_file)
    when ".yml"
      data = YAML.load(File.read(data_file))
    when ".json"
      data = MultiJson.decode(File.read(data_file))
    else
      data = {}
    end
  else
    data = {}
  end
end

#relative_pathObject



75
76
77
# File 'lib/soundwave.rb', line 75

def relative_path
  @path.relative_path_from(site.source).to_s
end

#renderObject



87
88
89
# File 'lib/soundwave.rb', line 87

def render
  Soundwave::Mustache.new(self).render(@path.read, self.read_data)
end

#write(destination) ⇒ Object



91
92
93
94
95
# File 'lib/soundwave.rb', line 91

def write(destination)
  destination = Pathname(destination)
  puts "#{relative_path} => #{destination.relative_path_from(site.source)}"
  File.open(destination, "w") { |f| f.write(self.render) }
end