Sha256: 0b24ffb1969876d79066be93e7e0701075241c63e1c77dde95a1284ab1464c85

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

require 'capgun/creatable'

module Capgun
  class Base
    attr_accessor :attrs
    alias :to_hash :attrs

    # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
    #
    # @overload self.lazy_attr_reader(attr)
    #   @param attr [Symbol]
    # @overload self.lazy_attr_reader(attrs)
    #   @param attrs [Array<Symbol>]
    def self.lazy_attr_reader(*attrs)
      attrs.each do |attribute|
        class_eval do
          define_method attribute do
            @attrs[attribute.to_s]
          end
        end
      end
    end

    # Initializes a new Base object
    #
    # @param attrs [Hash]
    # @return [Capgun::Base]
    def initialize(attrs={})
      @attrs = attrs.dup
    end

    # Initializes a new Base object
    #
    # @param method [String, Symbol] Message to send to the object
    def [](method)
      self.__send__(method.to_sym)
    rescue NoMethodError
      nil
    end

    # @param other [Capgun::Base]
    # @return [Boolean]
    def ==(other)
      super || (other.class == self.class && other.id == self.id)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
capgun-0.2.0 lib/capgun/base.rb
capgun-0.1.2 lib/capgun/base.rb
capgun-0.1.1 lib/capgun/base.rb
capgun-0.1.0 lib/capgun/base.rb
capgun-0.0.3 lib/capgun/base.rb