Sha256: a164f8af94cd35faee9afe9548898132beb2f27f71f8983fe9bcc4b6250c0fc3

Contents?: true

Size: 967 Bytes

Versions: 3

Compression:

Stored size: 967 Bytes

Contents

require "peto/mixin/peto_errorable"

module PetoClass
  include PetoErrorable

  def set_args(args)
    args.each do |key, value|
      var = instance_variable_get("@#{key}")
      invalid_type(key, types[key], value) unless value.class == types[key] || value.nil?
      raise_errors unless errors.empty?
      instance_variable_set("@#{key}", value)
    end
  end

  def inspect
    "#{self.class}(#{to_hash})"
  end

  def to_hash
    inners = members.inject({}) {|result, member|
      result[member] = instance_variable_get("@#{member}")
      result
    }
  end

  def valid?
    to_hash.each do |key, value|
      var = instance_variable_get("@#{key}")
      invalid_type(key, types[key], value) unless value.class == types[key] || value.nil?
    end

    arrays.each do |array, klass|
      var = instance_variable_get("@#{array}")
      invalid_type(array, klass, var.first) unless var.empty? || var.first.class == klass
    end

    errors.empty?
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
peto-0.2.7 lib/peto/mixin/peto_class.rb
peto-0.2.6 lib/peto/mixin/peto_class.rb
peto-0.2.5 lib/peto/mixin/peto_class.rb