Sha256: 8d2b48e79ddf56f64761683af3d23a732fa2e2159c7ac29cde1a7e04b90da584

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 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

          add Fasten::Task.new({ name: name }.merge(params))
        end

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

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

        items = task_list.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

3 entries across 3 versions & 1 rubygems

Version Path
fasten-0.7.2 lib/fasten/support/yaml.rb
fasten-0.7.0 lib/fasten/support/yaml.rb
fasten-0.6.0 lib/fasten/support/yaml.rb