Sha256: 449cfdac8bfb302e8fcbdd301ea6f767ed51e48cc013782e907e27f94be38966

Contents?: true

Size: 1.28 KB

Versions: 11

Compression:

Stored size: 1.28 KB

Contents

module Flattr
  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

    def self.lazy_attr_writer(*attrs)
      attrs.each do |attribute|
        class_eval do
          define_method "#{attribute}=" do |a|
            if @attrs[attribute.to_s] != a.to_s
              @has_changed = true
              @changes.push(attribute)
            end
            @attrs[attribute.to_s] = a.to_s
          end
        end
      end
    end

    # Initializes a new Base object
    #
    # @param attrs [Hash]
    # @return [Flattr::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

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
flattr-0.3.7 lib/flattr/base.rb
flattr-0.3.6 lib/flattr/base.rb
flattr-0.3.5 lib/flattr/base.rb
flattr-0.3.4 lib/flattr/base.rb
flattr-0.3.3 lib/flattr/base.rb
flattr-0.3.2 lib/flattr/base.rb
flattr-0.3.1 lib/flattr/base.rb
flattr-0.3.0 lib/flattr/base.rb
flattr-0.2.3 lib/flattr/base.rb
flattr-0.2.2 lib/flattr/base.rb
flattr-0.2.1 lib/flattr/base.rb