Sha256: 5897581706fe22897bc2686ee4bb31acd7e828a0fd4fbd943abe80ac661e1e6d

Contents?: true

Size: 929 Bytes

Versions: 12

Compression:

Stored size: 929 Bytes

Contents

module Lookbook
  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

12 entries across 12 versions & 1 rubygems

Version Path
lookbook-1.5.5 lib/lookbook/support/store.rb
lookbook-1.5.4 lib/lookbook/support/store.rb
lookbook-1.5.3 lib/lookbook/support/store.rb
lookbook-1.5.2 lib/lookbook/support/store.rb
lookbook-1.5.1 lib/lookbook/support/store.rb
lookbook-1.5.0 lib/lookbook/support/store.rb
lookbook-1.4.5 lib/lookbook/support/store.rb
lookbook-1.4.4 lib/lookbook/support/store.rb
lookbook-1.4.3 lib/lookbook/support/store.rb
lookbook-1.4.2 lib/lookbook/support/store.rb
lookbook-1.4.1 lib/lookbook/support/store.rb
lookbook-1.4.0 lib/lookbook/support/store.rb