Sha256: 40b20760bab8ed94a5cc77a5688298f3c95ff761b7cf2a39c2cc72d493d7649e

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'active_support/inflector'
require 'json'

module SettingsJs
  class Merger

    # Public: Get all values for keys defined in settins and merged it to a hash.
    #
    # Examples
    #
    #   merger = SettingsJs::Merger.new
    #   merger.to_hash
    #   # => { key1: 'value', key2: { sub_key2: 'other value' }}
    #
    # Returns Hash.
    # Raises TypeError if keys defined in config is not a reduceable object (Array,...)
    def to_hash
      unless config.keys.respond_to?(:reduce)
        raise TypeError.new('the configuration key "keys" must be reduceable')
      end

      config.keys.reduce({}) { |hash, base_key| hash.merge(backend.to_hash(base_key)) }
    end

    # Public: Get all values for keys defined in settins and merged it to a json.
    #
    # Examples
    #
    #   merger = SettingsJs::Merger.new
    #   merger.to_hash
    #   # => { key1: 'value', key2: { sub_key2: 'other value' }}
    #   merger.to_json
    #   # => { 'key1': 'value', 'key2': { 'sub_key2': 'other value' }}
    #
    # Returns Json.
    def to_json
      to_hash.to_json
    end

  private

    def config
      SettingsJs::Config.instance
    end

    def backend
      backend_proxy_path = "settings_js/backends/#{config.backend}"

      require backend_proxy_path
      backend_proxy_path.classify.constantize.new(config.klass)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
settings_js-0.1.2 lib/settings_js/merger.rb