Sha256: 88e7907fd2a52faae1743167008786d247a8756037af16b771319dc4320fafe7

Contents?: true

Size: 1015 Bytes

Versions: 5

Compression:

Stored size: 1015 Bytes

Contents

module Nanoc

  # Nanoc::Proxy is used when making data available to pages and layouts.
  # It provides an easy way to access data without the risk of accidentally
  # calling destructive methods.
  class Proxy

    instance_methods.each { |m| undef_method m unless m =~ /^__/ }

    # Creates a proxy for the given object.
    def initialize(obj)
      @obj = obj
    end

    # Requests the attribute with the given name. +key+ can be a string or a
    # symbol, and it can contain a trailing question mark (which will be
    # stripped).
    def [](key)
      real_key = key.to_s.sub(/\?$/, '').to_sym

      @obj.attribute_named(real_key)
    end

    # Sets a given attribute. The use of setting an object's attributes is not
    # recommended but may be necessary in some cases.
    def []=(key, value)
      @obj.attributes[key.to_sym] = value
    end

    # Used for requesting attributes without accessing the proxy like a hash.
    def method_missing(method, *args)
      self[method]
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nanoc-2.1 lib/nanoc/base/proxy.rb
nanoc-2.1.1 lib/nanoc/base/proxy.rb
nanoc-2.1.2 lib/nanoc/base/proxy.rb
nanoc-2.1.3 lib/nanoc/base/proxy.rb
nanoc-2.1.4 lib/nanoc/base/proxy.rb