Sha256: 1af795d1232deb9b3bdd08f4a063ad1d37a4a60fc9d66cc383331abf22883d4d

Contents?: true

Size: 1.4 KB

Versions: 35

Compression:

Stored size: 1.4 KB

Contents

require "yaml"
require "relaton/cli/base_convertor"

module Relaton
  module Cli
    class YAMLConvertor < Relaton::Cli::BaseConvertor
      def to_xml
        if writable
          convert_and_write(file_content, :to_xml)
        else
          convert_content(file_content).to_xml
        end
      end

      # Convert to XML
      #
      # This interface allow us to convert any YAML file to XML.
      # It only require us to provide a valid YAML file and it can
      # do converstion using default attributes, but it also allow
      # us to provide custom options to customize this converstion
      # process.
      #
      # @param file [File] The complete path to a YAML file
      # @param options [Hash] Options as hash key, value pairs.
      #
      def self.to_xml(file, options = {})
        new(file, options).to_xml
      end

      private

      def default_ext
        "rxl"
      end

      def file_content
        YAML.load_file(file)
      end

      def convert_single_file(content)
        Relaton::Bibdata.new(content)
      end

      def convert_collection(content)
        if content.has_key?("root")
          Relaton::Bibcollection.new(content["root"])
        end
      end

      def xml_content(_raw_file)
        convert_content(file_content).to_xml
      end

      def convert_content(content)
        convert_collection(content) || convert_single_file(content)
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
relaton-cli-0.5.0 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.4.3 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.4.2 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.4.1 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.4.0 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.19 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.18 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.17 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.16 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.15 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.14 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.13 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.12 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.11 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.10 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.9 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.8 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.7 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.6 lib/relaton/cli/yaml_convertor.rb
relaton-cli-0.3.5 lib/relaton/cli/yaml_convertor.rb