Sha256: 25d5fcc7e48f9bccc647f8200fc0b05e83500ab54cd9eda67943c345fd29e538

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

module Asciimath2UnitsML
  class Conv
    def read_yaml(path)
      validate_yaml(symbolize_keys(YAML
        .load_file(File.join(File.join(File.dirname(__FILE__), path)))), path)
    end

    def symbolize_keys(hash)
      return hash if hash.is_a? String

      hash.inject({}) do |result, (key, value)|
        new_key = case key
                  when String then key.to_sym
                  else key
                  end
        new_value = case value
                    when Hash then symbolize_keys(value)
                    when Array then value.map { |m| symbolize_keys(m) }
                    else value
                    end
        result[new_key] = new_value
        result
      end
    end

    def flip_name_and_symbol(hash)
      hash.each_with_object({}) do |(_k, v), m|
        next if v.name.nil? || v.name.empty?

        m[v.symbolid] = v
      end
    end

    def flip_name_and_symbols(hash)
      hash.each_with_object({}) do |(_k, v), m|
        next if v.name.nil? || v.name.empty?

        v.symbolids.each { |s| m[s] = v }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
asciimath2unitsml-0.4.4 lib/asciimath2unitsml/read.rb
asciimath2unitsml-0.4.3 lib/asciimath2unitsml/read.rb
asciimath2unitsml-0.4.2 lib/asciimath2unitsml/read.rb
asciimath2unitsml-0.4.1 lib/asciimath2unitsml/read.rb
asciimath2unitsml-0.4.0 lib/asciimath2unitsml/read.rb