Class: Edison::ModelsLoader

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

Instance Method Summary collapse

Instance Method Details

#load(directory) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/edison.rb', line 31

def load(directory)
	klasses = Dir[File.join(directory, "*")].map &File.method(:basename)
	hash = Hashie::Mash.new
	klasses.each do |klass|
		objects = Dir[File.join(directory, klass, "*")]
		objects.map! do |fname|
			puts "Loading model #{File.join(klass, File.basename(fname))}..."

			data = Hashie::Mash.new(case File.extname(fname)
			when /(html)|(md)$/
				Edison::YAMLFrontMatter.read(fname)
			when /ya?ml$/
				YAML.load_file(fname)
			else
				{}
			end)

			if data._ext
				raise Exception, "Restricted key '_ext' appears in model!"
			end
			data._ext = File.extname(fname)

			if data._ext == ".md"
				data.body = Kramdown::Document.new(data.body).to_html
			end

			if data._fname
				raise Exception, "Restricted key '_fname' appears in model!"
			end
			data._fname = File.basename(fname).sub(/#{File.extname(fname)}$/,'')

			data
		end
		hash[klass] = objects
	end
	hash
end