Sha256: cad1db3e4479b7a30266a1059d739d8b0559e8934f060a9f3abc3fe70316090e

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Daijobu
  class NamespaceProxy
    
    @@default_separator = ':'
    
    # Getter for the default separator. Default is ':'
    def self.default_separator
      @@default_separator
    end
    
    # Setter for the default separator. I use ':', but a lot of people like '/'.
    def self.default_separator=(separator)
      @@default_separator = separator
    end

    # NamespaceProxy.new takes an owner (a Daijobu::Client), a namespace, and a separator (defaults 
    # to @@default_separator).
    # NamespaceProxy objects are typically created using Daijobu::Client#method_missing, so you're
    # rarely going to instantiate one of these on your own.
    def initialize(owner, namespace, separator = @@default_separator)
      @owner = owner
      @namespace = namespace.to_s
      @separator = separator
    end

    # Sends #[] back to the owner, prepending the key given with the namespace and separator.
    def [](key)
      @owner["#{@namespace}#{@separator}#{key}"]
    end

    # Sends #[]= back to the owner, prepending the key given with the namespace and separator.    
    def []=(key, value)
      @owner["#{@namespace}#{@separator}#{key}"] = value
    end

    # Any missing method is assumed to be yet another namespace.
    def method_missing(namespace, *args)
      separator = args.shift || @@default_separator
      @namespace = "#{@namespace}#{@separator}#{namespace}"
      @separator = separator
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sander6-daijobu-0.2.1 lib/daijobu/namespace_proxy.rb