Class: MVCLI::Validatable::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/mvcli/validatable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Validation

Returns a new instance of Validation.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mvcli/validatable.rb', line 89

def initialize(object)
  @object = object
  @children = Map.new do |h,k|
    h[k] = [] unless k.nil?
  end
  @violations = Map.new do |h,k|
    h[k] = [] unless k.nil?
  end
  @errors = Map.new do |h,k|
    h[k] = [] unless k.nil?
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



87
88
89
# File 'lib/mvcli/validatable.rb', line 87

def errors
  @errors
end

#objectObject (readonly)

Returns the value of attribute object.



87
88
89
# File 'lib/mvcli/validatable.rb', line 87

def object
  @object
end

#violationsObject (readonly)

Returns the value of attribute violations.



87
88
89
# File 'lib/mvcli/validatable.rb', line 87

def violations
  @violations
end

Instance Method Details

#[](key) ⇒ Object



117
118
119
# File 'lib/mvcli/validatable.rb', line 117

def [](key)
  @children[key]
end

#append(name, validations) ⇒ Object



125
126
127
# File 'lib/mvcli/validatable.rb', line 125

def append(name, validations)
  @children[name] += validations
end

#children_to_sObject



137
138
139
# File 'lib/mvcli/validatable.rb', line 137

def children_to_s
  Hash[@children.keys.zip @children.values.map {|validations| validations.map(&:to_s)}].inspect
end

#children_valid?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
# File 'lib/mvcli/validatable.rb', line 110

def children_valid?
  @children.values.each do |validations|
    return false unless validations.all?(&:valid?)
  end
  return true
end

#each(&block) ⇒ Object



121
122
123
# File 'lib/mvcli/validatable.rb', line 121

def each(&block)
  @children.each &block
end

#to_sObject



129
130
131
132
133
134
135
# File 'lib/mvcli/validatable.rb', line 129

def to_s
  elements = []
  elements << "violations: #{@violations.inspect}" unless @violations.empty?
  elements << "errors: #{@errors.inspect}" unless @errors.empty?
  elements << "nested: #{children_to_s}" unless @children.empty?
  [@object, elements.join(', ')].join ' '
end

#valid?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/mvcli/validatable.rb', line 102

def valid?
  violations.empty? && errors.empty? && children_valid?
end

#validate!Object



106
107
108
# File 'lib/mvcli/validatable.rb', line 106

def validate!
  fail ValidationError, self unless valid?
end