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.25 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.24 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.23 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.22 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.21 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.20 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.19 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.18 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.17 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.16 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.15 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.14 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.13 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.12 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.11 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.10 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.9 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.8 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.7 app/services/flexite/data/migrators/yaml.rb
flexite-0.0.6 app/services/flexite/data/migrators/yaml.rb