Module: PetoClass

Includes:
PetoErrorable
Defined in:
lib/peto/mixin/peto_class.rb

Instance Method Summary collapse

Methods included from PetoErrorable

#add_error, #errors, #invalid_type, #raise_errors

Instance Method Details

#inspectObject



15
16
17
# File 'lib/peto/mixin/peto_class.rb', line 15

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

#set_args(args) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/peto/mixin/peto_class.rb', line 6

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

#to_hashObject



19
20
21
22
23
24
# File 'lib/peto/mixin/peto_class.rb', line 19

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

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/peto/mixin/peto_class.rb', line 26

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