Sha256: 81231a11253f016afabed1008508f32d18a59f53488e746ca0e618c589d4c6d9

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'fathom'))
require 'open-uri'
require 'yaml'

class Fathom::YAMLImport < Import
  def import_plausible_ranges
    assert_yaml_content
    plausible_ranges = extract_plausible_ranges
    [PlausibleRange, plausible_ranges]
  end
  
  def import_data_nodes
    assert_yaml_content
    data_nodes = extract_data_nodes
    [DataNode, data_nodes]
  end
  
  protected
    def assert_yaml_content
      return @yaml_content if @yaml_content
      begin
        file_contents = open(self.content).read
        raise ArgumentError, "Unable to extract YAML data out of the contents." unless file_contents
      rescue
        file_contents = self.content
      end
        @yaml_content = YAML.load(file_contents)
    end
    
    def extract_plausible_ranges
      @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]
          list << value.merge(:name => name)
        end
        list 
      end
    end
    
    def extract_data_nodes
      @yaml_content.inject([]) do |list, array|
        name, value = array.first, array.last
        list << {:name => name, :values => value} if value.is_a?(Array)
        list 
      end
    end
end

if __FILE__ == $0
  include Fathom
  # TODO: Is there anything you want to do to run this file on its own?
  # YAMLImport.new
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fathom-0.2.3 lib/fathom/import/yaml_import.rb
fathom-0.2.2 lib/fathom/import/yaml_import.rb
fathom-0.2.1 lib/fathom/import/yaml_import.rb
fathom-0.2.0 lib/fathom/import/yaml_import.rb