Module: DataProcessor::Import

Included in:
DataProcessor
Defined in:
lib/data_processor/import.rb

Instance Method Summary collapse

Instance Method Details

#import(path = "", data_obj = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
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
# File 'lib/data_processor/import.rb', line 4

def import(path="", data_obj=nil)
  path = path.sub(/^\/+/, "")
  path = path.sub(/\/+$/, "")

  data_hash = data_obj || @data
  full_path = File.join(@initial_path, path)

  Dir.foreach(full_path) do |filename|
    next if filename[0] == "."
    combined_path = File.join(full_path, filename)

    # directory
    if File.directory?(combined_path)
      data_hash[filename] ||= {}
      data_hash[filename].merge!(import(File.join(path, filename), data_hash[filename]))
      data_hash[filename].merge!({ "is_directory" => true })

    # yaml file
    elsif filename.end_with?(".yml")
      without_ext = File.basename(filename, ".yml")
      yaml_data = YAML.load_file(combined_path) || {}

      data_hash[without_ext] ||= {}
      data_hash[without_ext].merge!(yaml_data)
      data_hash[without_ext].merge!({ "is_yaml" => true })

    # markdown file
    elsif filename.end_with?(".md")
      without_ext = File.basename(filename, ".md")
      file = File.open(combined_path, "r")
      parsed_markdown = self.parse_markdown(file.read)
      file.close

      data_hash[without_ext] ||= {}
      data_hash[without_ext].merge!({ "parsed_markdown" => parsed_markdown })
      data_hash[without_ext].merge!({ "is_markdown" => true })

    end
  end

  data_hash
end