Module: DocumentMapper::YamlParsing

Included in:
Document
Defined in:
lib/document_mapper/yaml_parsing.rb

Instance Method Summary collapse

Instance Method Details

#read_yamlObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/document_mapper/yaml_parsing.rb', line 14

def read_yaml
  file_path = attributes[:file_path]
  file_content = File.read(file_path)

  if file_content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    @content = file_content[(Regexp.last_match(1).size + Regexp.last_match(2).size)..]
    attributes.update(yaml_load(Regexp.last_match(1), file_path).transform_keys(&:to_sym))
  end

  file_name = File.basename(file_path)
  extension = File.extname(file_path)
  attributes.update({
                      file_name: file_name,
                      extension: extension.sub(/^\./, ''),
                      file_name_without_extension: File.basename(file_path, extension)
                    })

  unless attributes.key? :date
    begin
      match = attributes[:file_name].match(/(\d{4})-(\d{1,2})-(\d{1,2}).*/)
      year = match[1].to_i
      month = match[2].to_i
      day = match[3].to_i
      attributes[:date] = ::Date.new(year, month, day)
    rescue NoMethodError
    end
  end

  if attributes.key? :date
    attributes[:year] = attributes[:date].year
    attributes[:month] = attributes[:date].month
    attributes[:day] = attributes[:date].day
  end

  self.class.define_attribute_methods attributes.keys
  attributes.each_key { |attr| self.class.define_read_method attr }
end

#yaml_load(yaml, file) ⇒ Object



52
53
54
55
56
# File 'lib/document_mapper/yaml_parsing.rb', line 52

def yaml_load(yaml, file)
  YAML.safe_load(yaml, permitted_classes: PERMITTED_CLASSES)
rescue ArgumentError, Psych::SyntaxError
  raise YamlParsingError, "Unable to parse YAML of #{file}"
end