Sha256: 3627a2fa52517b7995bc5987a557d9fee9631ea7289ef0708bf948a7a1218aeb

Contents?: true

Size: 1.3 KB

Versions: 24

Compression:

Stored size: 1.3 KB

Contents

module Flexite
  class Data::Migrators::Yaml
    def initialize
      @paths = Flexite.config.paths
      @roots = Flexite.config.source_roots
      @hierarchy = Flexite.config.hierarchy
      @data = Data::Hash.new
    end

    def call
      @paths.each_with_object(@data) do |(name, paths), data|
        paths.each do |p|
          next unless File.exists?(p)
          load_data(data, name, p)
        end
      end
    end

    private

    def load_data(data, name, path)
      with_hierarchy(path) do |configs|
        with_source_roots(data[name], configs, @roots[name])
      end
    end

    def with_source_roots(data, configs, roots)
      data.deep_merge!(configs) and return if roots.blank?

      roots.each do |root|
        next if configs[root].blank?
        data.deep_merge!(root => configs[root])
      end
    end

    # hierarchy = [:a, :b, :c]
    #
    # {
    #   a: {
    #         b: {
    #              c: {}
    #            }
    #      }
    # }
    #
    #

    def with_hierarchy(path)
      hierarchy = @hierarchy[path]
      return yield(YAML.load_file(path)) if hierarchy.blank?

      new_hash = {}
      leveling = new_hash
      hierarchy.each do |level|
        leveling = leveling[level] = {}
      end

      leveling.merge!(YAML.load_file(path))
      yield(new_hash)
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
flexite-0.0.5 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.4 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.3 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.2 app/services/flexite/data/migrators/yaml.rb