Module: Struct::Validatable::SubclassInstanceMethods

Defined in:
lib/struct/validatable/subclass_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#[]=(key, value) ⇒ Object

Parameters:

  • key (Symbol, String, #to_sym, Integer, #to_int)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/struct/validatable/subclass_instance_methods.rb', line 23

def []=(key, value)
  autonym = __class__.autonym_for_key(key)

  if __class__.with_adjuster?(autonym)
    begin
      value = instance_exec(value, &__class__.adjuster_for(autonym))
    rescue Exception
      raise Struct::Validatable::InvalidAdjustingError
    end
  end

  if __class__.with_condition?(autonym)
    unless _valid?(__class__.condition_for(autonym), value)
      raise Struct::Validatable::InvalidWritingError,
            "#{value.inspect} is deficient for #{key}(#{autonym})"
    end
  end

  super
end

#initialize(*values, **kw_args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/struct/validatable/subclass_instance_methods.rb', line 9

def initialize(*values, **kw_args)
  super
  if kw_args.empty?
    values.each_with_index do |val, idx|
      self[idx] = val
    end
  else
    kw_args.each_pair do |kw, value|
      self[kw] = value
    end
  end
end

#valid?(key, value = ) ⇒ Boolean

Parameters:

  • key (Symbol, String, #to_sym, Integer, #to_int)

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
# File 'lib/struct/validatable/subclass_instance_methods.rb', line 45

def valid?(key, value=self[key])
  return true unless __class__.restrict?(key)

  begin
    _valid?(__class__.condition_for(key), value)
  rescue Exception
    false
  end
end