Sha256: 270baf4914a0f7013d395f0416211fa944d421e8aeeba3271f5801c73cd97261

Contents?: true

Size: 1016 Bytes

Versions: 5

Compression:

Stored size: 1016 Bytes

Contents

module Lookbook
  class Store < ActiveSupport::OrderedOptions
    def initialize(data = {}, deep = false)
      super()
      @deep = deep
      set(data) if data.present?
    end

    def [](key)
      super(normalize_key(key))
    end

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

    def set(data)
      data.keys.each do |key|
        self[normalize_key(key)] = normalize_value(data[key])
      end
      self
    end

    def get(key, fallback = nil)
      if key?(normalize_key(key))
        self[normalize_key(key)]
      else
        fallback
      end
    end

    def method_missing(name, *args)
      super(normalize_key(name), *args.map { |arg| normalize_value(arg) })
    end

    def respond_to_missing?(name, *)
      key?(name)
    end

    def normalize_key(key)
      key.to_s.downcase.tr("-", "_").to_sym
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lookbook-1.1.0 lib/lookbook/store.rb
lookbook-1.0.8 lib/lookbook/store.rb
lookbook-1.0.7 lib/lookbook/store.rb
lookbook-1.0.6 lib/lookbook/store.rb
lookbook-1.0.5 lib/lookbook/store.rb