lib/fathom/import/yaml_import.rb in fathom-0.3.0 vs lib/fathom/import/yaml_import.rb in fathom-0.3.1
- old
+ new
@@ -1,10 +1,10 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom'))
require 'open-uri'
require 'yaml'
-class Fathom::YAMLImport < Import
+class Fathom::YAMLImport < Fathom::Import
def import_plausible_ranges
assert_yaml_content
plausible_ranges = extract_plausible_ranges
[PlausibleRange, plausible_ranges]
end
@@ -13,10 +13,16 @@
assert_yaml_content
data_nodes = extract_data_nodes
[DataNode, data_nodes]
end
+ def import_facts
+ assert_yaml_content
+ facts = extract_facts
+ [Fact, facts]
+ end
+
protected
def assert_yaml_content
return @yaml_content if @yaml_content
begin
file_contents = open(self.content).read
@@ -25,11 +31,25 @@
file_contents = self.content
end
@yaml_content = YAML.load(file_contents)
end
+ def extract_facts
+ return [] unless @yaml_content
+ output = []
+ @yaml_content.each do |e|
+ next unless e.is_a?(Array)
+ next unless e.first.to_s.match(/fact/i)
+ obj = e[1]
+ next unless obj.is_a?(Hash)
+ output << obj
+ end
+ output
+ end
+
def extract_plausible_ranges
+ return [] unless @yaml_content
@yaml_content.inject([]) do |list, array|
name, value = array.first, array.last
if value.is_a?(Hash)
value = OptionsHash.new(value)
# list << value.merge(:name => name) if value[:min] and value[:max]
@@ -38,9 +58,10 @@
list
end
end
def extract_data_nodes
+ return [] unless @yaml_content
@yaml_content.inject([]) do |list, array|
name, value = array.first, array.last
list << {:name => name, :values => value} if value.is_a?(Array)
list
end