Sha256: 371112e8ed202b891e99301f0fb7920d44e6ede6e7cb23588d6217655120e0d8

Contents?: true

Size: 982 Bytes

Versions: 2

Compression:

Stored size: 982 Bytes

Contents

#
# @abstract Subclass and define attributes
#
# @author Andreas Eger
class InterfaceHelper
  @@_attributes = Hash.new { |hash, key| hash[key] = [] }

  #
  # creates setter/getter similar to attr_accesor
  # @param  name [Symbol]
  # @macro [attach] attribute
  #   @method $1
  #     reads $1
  #   @method $1=
  #     saves $1
  def self.attribute name
    define_method(name) do
      @_attributes[name]
    end
    define_method(:"#{name}=") do |v|
      @_attributes[name] = v
    end
    attributes << name unless attributes.include? name
  end
  def self.attributes
    @@_attributes[self]
  end
  def initialize(params={})
    @_attributes = {}
    params.each do |key, value|
      send("#{key}=", value)
    end
  end

  #
  # custom comperator
  # @param anOther [InterfaceHelper]
  #
  # @return [Boolean] result after comparing each attribute
  def == anOther
    @_attributes.keys.map{ |sym| self.send(sym) == anOther.send(sym)}.reduce(true){|a,e| a && e }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
svm_helper-0.2.1 lib/svm_helper/interface_helper.rb
svm_helper-0.1.1 lib/svm_helper/interface_helper.rb