Class: Rov::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/rov/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validate_obj) ⇒ Template

Returns a new instance of Template.



30
31
32
33
34
# File 'lib/rov/template.rb', line 30

def initialize(validate_obj)
  @validate_obj = validate_obj
  @raw_template_value = self.class.template
  @required = self.class.required
end

Class Method Details

.any_of(lst) ⇒ Object




230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rov/template.rb', line 230

def self.any_of(lst)
  any_of_cls = Class.new(Rov::Template) do
    @template = AnyOfArray.new(lst)
    def validate_method
      m = lambda do |actual_value|
        is_exist = false
        self.get_template_value_for_validation.each do |_template_value|
          if _template_value.do_validate(actual_value)[0]
            is_exist = true
            break
          end
        end
        if not is_exist
          @validate_obj.raise_validation_error(:not_include)
        end
        [true, ""]
      end
      return m
    end
  end
  return any_of_cls
end

.anythingObject



304
305
306
# File 'lib/rov/template.rb', line 304

def self.anything
  return kind_of(Object)
end

.create_template(template_value) ⇒ Object



7
8
9
10
11
12
# File 'lib/rov/template.rb', line 7

def self.create_template(template_value)
  template_cls = Class.new(self) do
    @template = template_value
  end
  return template_cls
end

.in_range(_range) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rov/template.rb', line 287

def self.in_range(_range)
  in_range_cls = Class.new(Rov::Template) do
    @template = _range

    def validate_method
      m = lambda do |actual_value|
        if not self.get_template_value.include?(actual_value)
          @validate_obj.raise_validation_error(:not_in_range)
        end
        [true, ""]
      end
      return m
    end
  end
  return in_range_cls
end

.instance_of(cls) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rov/template.rb', line 253

def self.instance_of(cls)
  instance_of_cls = Class.new(Rov::Template) do
    @template = cls

    def validate_method
      m = lambda do |actual_value|
        if not actual_value.instance_of?(self.get_template_value)
          @validate_obj.raise_validation_error(:type_error)
        end
        [true, ""]
      end
      return m
    end
  end
  return instance_of_cls
end

.kind_of(cls) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rov/template.rb', line 270

def self.kind_of(cls)
  kind_of_cls = Class.new(Rov::Template) do
    @template = cls

    def validate_method
      m = lambda do |actual_value|
        if not actual_value.is_a?(self.get_template_value)
          @validate_obj.raise_validation_error(:type_error)
        end
        [true, ""]
      end
      return m
    end
  end
  return kind_of_cls
end

.ordered?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rov/template.rb', line 26

def self.ordered?
  return @ordered
end

.requiredObject



22
23
24
# File 'lib/rov/template.rb', line 22

def self.required
  return @required.to_a
end

.templateObject



14
15
16
# File 'lib/rov/template.rb', line 14

def self.template
  return @template
end

.template=(value) ⇒ Object



18
19
20
# File 'lib/rov/template.rb', line 18

def self.template=(value)
  @template = value
end

Instance Method Details

#do_custom_validate(actual_value) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/rov/template.rb', line 206

def do_custom_validate(actual_value)
  if get_validate_option[:json]
    case
    when actual_value.is_a?(Hash)
      actual_value = Common.with_symbol_access(actual_value)
    when actual_value.is_a?(Array)
      actual_value = Common.string_array_element(actual_value)
    when actual_value.is_a?(Symbol)
      actual_value = actual_value.to_s
    end
  end

  return self.validate(actual_value)
end

#get_template_valueObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rov/template.rb', line 44

def get_template_value
  if @template_value
    return @template_value
  end
  value = @raw_template_value
  validate_option = get_validate_option()

  if validate_option[:json]
    if value.is_a?(Hash)
      value = Common.string_hash_key(value)
      value = Common.with_symbol_access(value)
    elsif value.is_a?(Array)
      value = Common.string_array_element(value)
    elsif value.is_a?(Symbol)
      value = value.to_s
    elsif value == Symbol
      value = String
    end
  end

  @template_value = value
  return @template_value
end

#get_template_value_for_validationObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rov/template.rb', line 68

def get_template_value_for_validation
  if @template_value_for_validation
    return @template_value_for_validation
  end
  value = get_template_value()
  case
  when value.is_a?(Hash)
    value = Hash[
                 value.map do |k, v|
                   if Common.template_cls?(k)
                     k = @validate_obj.class.new(k)
                   end
                   [k, @validate_obj.class.new(v)]
                 end
                ]
  when value.is_a?(Array)
    value = value.map {|a| @validate_obj.class.new(a)}
  # else
  #   if value.class == @validate_obj.class.superclass
  #     # clone_self(value)
  #     value = value
  #   else
  #     # @template_value = value
  #     value = value
  #   end
  end
  @template_value_for_validation = value
  return @template_value_for_validation
end

#get_validate_optionObject



36
37
38
# File 'lib/rov/template.rb', line 36

def get_validate_option
  return @validate_obj.get_option()
end

#ordered?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/rov/template.rb', line 202

def ordered?
  return self.class.ordered?
end

#raise_validation_error(error_type) ⇒ Object



225
226
227
# File 'lib/rov/template.rb', line 225

def raise_validation_error(error_type)
  return @validate_obj.raise_validation_error(error_type)
end

#requiredObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rov/template.rb', line 187

def required
  _required = @required
  if get_validate_option[:json]
    case
    when _required.is_a?(Hash)
      _required = Common.string_hash_key(_required)
    when _required.is_a?(Array)
      _required = Common.string_array_element(_required)
    when _required.is_a?(Symbol)
      _required = _required.to_s
    end
  end
  return _required
end

#set_template_value(value) ⇒ Object



40
41
42
# File 'lib/rov/template.rb', line 40

def set_template_value(value)
  @raw_template_value = value
end

#validate(actual_value) ⇒ Object



221
222
223
# File 'lib/rov/template.rb', line 221

def validate(actual_value)
  return true
end

#validate_array(actual_array) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rov/template.rb', line 153

def validate_array(actual_array)
  _template_value = get_template_value_for_validation
  if actual_array.class != _template_value.class
    @validate_obj.raise_validation_error(:type_error)
  end

  if self.ordered?
    actual_array.zip(_template_value).each do |real_element, template_element|
      if template_element.nil?
        @validate_obj.set_value_hash(:actual_array_element => real_element)
        @validate_obj.raise_validation_error(:surplus_element)
      end
      template_element.validate(real_element)
    end
  else
    actual_array.each do |re|
      if _template_value.map {|te| te.do_validate(re)[0]}.none?
        @validate_obj.raise_validation_error(:invalid_element)
      end
    end
    self.validate_required(actual_array)
  end

  return [true, ""]
end

#validate_hash(actual_hash) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rov/template.rb', line 125

def validate_hash(actual_hash)
  _template_value = get_template_value_for_validation
  if actual_hash.class != _template_value.class
    @validate_obj.raise_validation_error(:type_error)
  end
  actual_hash.each_pair do |k, v|
    @validate_obj.set_value_hash(:actual_hash_key => k)
    if _template_value.keys.include?(k)
      _template_value[k].validate(v)
    else
      is_exist = false
      (_template_value.keys.find_all {|p| p.class == @validate_obj.class.superclass or p.class == @validate_obj.class}).each do |d|
        if d.do_validate(k)[0]
          _template_value[d].validate(v)
          is_exist = true
        end
      end
      if not is_exist
        @validate_obj.raise_validation_error(:invalid_key)
      end
    end
  end

  self.validate_required(actual_hash.keys)

  return [true, ""]
end

#validate_methodObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rov/template.rb', line 98

def validate_method
  value = get_template_value_for_validation
  case
  when value.is_a?(Hash)
    m = method(:validate_hash)
  when value.is_a?(Array)
    m = method(:validate_array)
  else
    if value.is_a?(Class) and value.superclass == self.class.superclass
      m = value.new(@validate_obj).validate_method
    else
      m = method(:validate_other)
    end
  end

  return m
end

#validate_other(actual_value) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/rov/template.rb', line 179

def validate_other(actual_value)
  _template_value = get_template_value_for_validation
  if not _template_value == actual_value
    @validate_obj.raise_validation_error(:not_eq)
  end
  return [true, ""]
end

#validate_required(array) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/rov/template.rb', line 116

def validate_required(array)
  self.required.each do |key|
    if not @validate_obj.class.new(array).do_validate([key])[0]
      @validate_obj.set_value_hash(:required_key => key)
      @validate_obj.raise_validation_error(:not_required)
    end
  end
end