Sha256: c1a4b6b5a7265378e713bdf4242e92b997d016ba984f0fcc8082cac7c0e47333

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require 'dslh'

module Rezept
  class Converter

    def set_options(options)
      @options = options
    end

    def to_dsl_all(docs)
      dsls = []
      docs.each do |doc|
        dsls << to_dsl(doc)
      end
      dsls.join("\n")
    end

    def to_dsl(doc)
      exclude_key = proc do |k|
        false
      end

      key_conv = proc do |k|
        k = k.to_s
        if k !~ /\A[_a-z]\w+\Z/i
          "_(#{k.inspect})"
        else
          k
        end
      end

      name = doc['name']
      document_type = doc['document_type']

      hash = {}
      hash['account_ids'] = doc['account_ids']
      hash['content'] = doc['content']

      if doc['content'].kind_of?(String)
        if @options['dsl_content']
          hash['content'] = { __dsl: JSON.parse(doc['content']) }
        else
          hash['content'] = doc['content'].gsub(/\R$/, '')
        end
      end

      dsl = Dslh.deval(
        hash,
        exclude_key: exclude_key,
        use_heredoc_for_multi_line: true,
        key_conv: key_conv,
        initial_depth: 1
      )
<<-EOS
#{document_type} #{name.inspect} do
#{dsl}
end
EOS
    end

    def dslfile_to_h(dsl_file)
      context = DSLContext.new
      context.eval_dsl(dsl_file)
    end

    def filename(name)
      name.gsub!(/\W+/, '_')
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rezept-0.4.2 lib/rezept/converter.rb
rezept-0.4.1 lib/rezept/converter.rb
rezept-0.4.0 lib/rezept/converter.rb
rezept-0.3.1 lib/rezept/converter.rb
rezept-0.3.0 lib/rezept/converter.rb
rezept-0.2.0 lib/rezept/converter.rb
rezept-0.1.0 lib/rezept/converter.rb
rezept-0.0.2 lib/rezept/converter.rb