Sha256: 88574bc9e4ea0c2e9643ee95f9c48185b780d9c6f9bb8c2f808d9c18d147c7a6

Contents?: true

Size: 1021 Bytes

Versions: 2

Compression:

Stored size: 1021 Bytes

Contents

module EventedAttributes

  def has_attributes(*names)
    if names.first.is_a? Hash
      names.first.each do |name, default|
        has_attribute name, default
      end
    else
      names.each do |name|
        has_attribute name
      end
    end
  end

  def has_attribute(name, value=nil)
    @evented_attributes ||= []
    unless has_attribute? name
      @evented_attributes << name
      self.metaclass.send :kvo_attr_accessor, name
      self.define_singleton_method "#{name}?" do
        self.send name
      end
      self.send("#{name}=", value)
    end
  end

  def has_attribute?(name)
    @evented_attributes && @evented_attributes.include?(name)
  end

  def attributes
    {}.tap do |atts|
      if @evented_attributes
        @evented_attributes.each do |name|
          atts[name] = self.send name
        end
      end
    end
  end

  def self.included(klass)
    klass.instance_eval do
      include Kvo
      def has_attribute(name)
        kvo_attr_accessor name
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gamebox-0.4.0.rc2 lib/gamebox/lib/evented_attributes.rb
gamebox-0.4.0.rc1 lib/gamebox/lib/evented_attributes.rb