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.



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

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

Instance Attribute Details

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#object ⇒ Object (readonly)

Returns the value of attribute object.



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

def object
  @object
end

#violations ⇒ Object (readonly)

Returns the value of attribute violations.



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

def violations
  @violations
end

Instance Method Details

#[](key) ⇒ Object



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

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

#append(name, validations) ⇒ Object



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

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

#children_to_s ⇒ Object



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

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

#children_valid? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#each(&block) ⇒ Object



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

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

#to_s ⇒ Object



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

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)


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

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

#validate! ⇒ Object



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

def validate!
  fail ValidationError, self unless valid?
end