Sha256: c1c13671db38c75539f51d3237226741a372e7f11e07caeba17034c478a476cd
Contents?: true
Size: 1.53 KB
Versions: 1
Compression:
Stored size: 1.53 KB
Contents
module DocumentMapper class YamlParsingError < StandardError; end module YamlParsing def read_yaml file_path = self.attributes[:file_path] @content = File.read(file_path) if @content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m @content = @content[($1.size + $2.size)..-1] self.attributes.update(yaml_load($1, file_path).transform_keys(&:to_sym)) end file_name = File.basename(file_path) extension = File.extname(file_path) self.attributes.update({ :file_name => file_name, :extension => extension.sub(/^\./, ''), :file_name_without_extension => File.basename(file_path, extension) }) if !self.attributes.has_key? :date begin match = attributes[:file_name].match(/(\d{4})-(\d{1,2})-(\d{1,2}).*/) year, month, day = match[1].to_i, match[2].to_i, match[3].to_i self.attributes[:date] = Date.new(year, month, day) rescue NoMethodError => err end end if self.attributes.has_key? :date self.attributes[:year] = self.attributes[:date].year self.attributes[:month] = self.attributes[:date].month self.attributes[:day] = self.attributes[:date].day end self.class.define_attribute_methods self.attributes.keys self.attributes.keys.each { |attr| self.class.define_read_method attr } end def yaml_load(yaml, file) YAML.load(yaml) rescue ArgumentError, Psych::SyntaxError raise YamlParsingError, "Unable to parse YAML of #{file}" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
document_mapper-0.2.1 | lib/document_mapper/yaml_parsing.rb |