Sha256: c742edb78810620f9fb16edcfc09c4ae826914bbd86dbb2b3f28a6ded44fb47c

Contents?: true

Size: 985 Bytes

Versions: 9

Compression:

Stored size: 985 Bytes

Contents

module Lookbook
  class Store < ActiveSupport::OrderedOptions
    def initialize(data = {}, deep = true)
      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?(Hash) ? Store.new(value) : value
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lookbook-1.0.4 lib/lookbook/store.rb
lookbook-1.0.3 lib/lookbook/store.rb
lookbook-1.0.2 lib/lookbook/store.rb
lookbook-1.0.1 lib/lookbook/store.rb
lookbook-1.0.0 lib/lookbook/store.rb
lookbook-1.0.0.rc.3 lib/lookbook/store.rb
lookbook-1.0.0.rc.2 lib/lookbook/store.rb
lookbook-1.0.0.rc.1 lib/lookbook/store.rb
lookbook-1.0.0.beta.8 lib/lookbook/store.rb