Sha256: 8bbb51fdb3cd3bab229d30852e5c9fe4f7f82157ccf74a5872f7f89de8669f0b

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

require 'yaml'

module Fasten
  module Support
    module Yaml
      def load_yaml(path)
        items = YAML.safe_load(File.read(path)).each do |name, params|
          if params.is_a? String
            params = { after: params }
          elsif params.is_a? Hash
            transform_params(params)
          else
            params = {}
          end

          task name, params
        end

        log_info "Loaded #{items.count} tasks from #{path}"
      end

      def save_yaml(path)
        keys = %i[after shell]

        items = tasks.map do |task|
          data = task.to_h.select do |key, _val|
            keys.include? key
          end

          [task.name, data]
        end.to_h

        File.write path, items.to_yaml

        log_info "Loaded #{items.count} tasks into #{path}"
      end

      protected

      def transform_params(params)
        params.keys.each do |k|
          val = params[k]

          if val.is_a?(String) && (match = %r{^/(.+)/$}.match(val))
            val = Regexp.new(match[1])
          end

          params[k.to_sym] = val
          params.delete(k)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
fasten-0.14.2 lib/fasten/support/yaml.rb
fasten-0.14.0 lib/fasten/support/yaml.rb
fasten-0.12.8 lib/fasten/support/yaml.rb
fasten-0.12.6 lib/fasten/support/yaml.rb
fasten-0.12.4 lib/fasten/support/yaml.rb
fasten-0.12.2 lib/fasten/support/yaml.rb
fasten-0.12.0 lib/fasten/support/yaml.rb
fasten-0.10.0 lib/fasten/support/yaml.rb
fasten-0.8.8 lib/fasten/support/yaml.rb
fasten-0.8.6 lib/fasten/support/yaml.rb
fasten-0.8.4 lib/fasten/support/yaml.rb
fasten-0.8.0 lib/fasten/support/yaml.rb
fasten-0.7.6 lib/fasten/support/yaml.rb
fasten-0.7.4 lib/fasten/support/yaml.rb