Sha256: 28cd37658867d9542a0ddafe045bab172897f3922068e8d62a96af62f33e624b

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Nanoc2

  # Nanoc2::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 =~ /^__/ || m.to_s == 'object_id' }

    # 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

1 entries across 1 versions & 1 rubygems

Version Path
nanoc2-2.2.3 lib/nanoc2/base/proxy.rb