Sha256: c26858d8a24d665f7eb048c67ad0781e4ab41690abf7ebd12a52466df624e3b8

Contents?: true

Size: 1.21 KB

Versions: 28

Compression:

Stored size: 1.21 KB

Contents

module Lookbook
  # Generic hash-like key/value store.
  #
  # Properties can be get/set using hash access syntax (`data[:key]`)
  # or dot-notation syntax (`data.key`).
  #
  # Based on [ActiveSupport::OrderedOptions](https://api.rubyonrails.org/classes/ActiveSupport/OrderedOptions.html)
  #
  # @ignore methods
  # @api public
  class Store < ActiveSupport::OrderedOptions
    def initialize(initial_data = nil, opts = {})
      @recursive = opts[:recursive] || false
      super()
      initial_data.to_h.each { |k, v| self[k] = v }
    end

    def []=(key, value)
      super(key, normalize_value(value))
    end

    def fetch(name, *args)
      super(name.to_sym, *args)
    end

    def method_missing(name, *args)
      if name.to_s.end_with?("=")
        args[0] = normalize_value(args[0])
      end
      super(name, *args)
    end

    def respond_to_missing?(name, include_private)
      true
    end

    def to_h
      transform_values do |value|
        value.is_a?(Store) ? value.to_h : value
      end
    end

    def to_hash
      to_h
    end

    protected

    def normalize_value(value)
      (@recursive && !value.is_a?(Store) && value.is_a?(Hash)) ? Store.new(value, recursive: @recursive) : value
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/support/store.rb
lookbook-2.3.3 lib/lookbook/support/store.rb
lookbook-2.3.2 lib/lookbook/support/store.rb
lookbook-2.3.0 lib/lookbook/support/store.rb
lookbook-2.2.2 lib/lookbook/support/store.rb
lookbook-2.2.1 lib/lookbook/support/store.rb
lookbook-2.2.0 lib/lookbook/support/store.rb
lookbook-2.1.1 lib/lookbook/support/store.rb
lookbook-2.1.0 lib/lookbook/support/store.rb
lookbook-2.0.5 lib/lookbook/support/store.rb
lookbook-2.0.4 lib/lookbook/support/store.rb
lookbook-2.0.3 lib/lookbook/support/store.rb
lookbook-2.0.2 lib/lookbook/support/store.rb
lookbook-2.0.1 lib/lookbook/support/store.rb
lookbook-2.0.0 lib/lookbook/support/store.rb
lookbook-2.0.0.rc.3 lib/lookbook/support/store.rb
lookbook-2.0.0.rc.2 lib/lookbook/support/store.rb
lookbook-2.0.0.rc.1 lib/lookbook/support/store.rb
lookbook-2.0.0.beta.9 lib/lookbook/support/store.rb
lookbook-2.0.0.beta.8 lib/lookbook/support/store.rb