Class: Kwalify::Rule

Inherits:
Object
  • Object
show all
Includes:
ErrorHelper
Defined in:
lib/kwalify/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorHelper

#_build_message, #assert_error, #schema_error, #validate_error

Constructor Details

#initialize(hash = nil, parent = nil) ⇒ Rule

Returns a new instance of Rule.



16
17
18
19
# File 'lib/kwalify/rule.rb', line 16

def initialize(hash=nil, parent=nil)
   init(hash, "", {}) if hash
   @parent = parent
end

Instance Attribute Details

#assertObject (readonly)

Returns the value of attribute assert.



33
34
35
# File 'lib/kwalify/rule.rb', line 33

def assert
  @assert
end

#assert_procObject (readonly)

Returns the value of attribute assert_proc.



34
35
36
# File 'lib/kwalify/rule.rb', line 34

def assert_proc
  @assert_proc
end

#classnameObject (readonly)

Returns the value of attribute classname.



39
40
41
# File 'lib/kwalify/rule.rb', line 39

def classname
  @classname
end

#descObject (readonly)

Returns the value of attribute desc.



24
25
26
# File 'lib/kwalify/rule.rb', line 24

def desc
  @desc
end

#enumObject (readonly)

Returns the value of attribute enum.



25
26
27
# File 'lib/kwalify/rule.rb', line 25

def enum
  @enum
end

#identObject (readonly)

Returns the value of attribute ident.



37
38
39
# File 'lib/kwalify/rule.rb', line 37

def ident
  @ident
end

#lengthObject (readonly)

Returns the value of attribute length.



36
37
38
# File 'lib/kwalify/rule.rb', line 36

def length
  @length
end

#mappingObject (readonly)

Returns the value of attribute mapping.



32
33
34
# File 'lib/kwalify/rule.rb', line 32

def mapping
  @mapping
end

#nameObject (readonly)

attr_reader :id



23
24
25
# File 'lib/kwalify/rule.rb', line 23

def name
  @name
end

#parentObject

Returns the value of attribute parent.



21
22
23
# File 'lib/kwalify/rule.rb', line 21

def parent
  @parent
end

#patternObject (readonly)

Returns the value of attribute pattern.



29
30
31
# File 'lib/kwalify/rule.rb', line 29

def pattern
  @pattern
end

#rangeObject (readonly)

Returns the value of attribute range.



35
36
37
# File 'lib/kwalify/rule.rb', line 35

def range
  @range
end

#regexpObject (readonly)

Returns the value of attribute regexp.



30
31
32
# File 'lib/kwalify/rule.rb', line 30

def regexp
  @regexp
end

#requiredObject (readonly)

Returns the value of attribute required.



26
27
28
# File 'lib/kwalify/rule.rb', line 26

def required
  @required
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



31
32
33
# File 'lib/kwalify/rule.rb', line 31

def sequence
  @sequence
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/kwalify/rule.rb', line 27

def type
  @type
end

#type_classObject (readonly)

Returns the value of attribute type_class.



28
29
30
# File 'lib/kwalify/rule.rb', line 28

def type_class
  @type_class
end

#uniqueObject (readonly)

Returns the value of attribute unique.



38
39
40
# File 'lib/kwalify/rule.rb', line 38

def unique
  @unique
end

Instance Method Details

#init(hash, path = "", rule_table = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kwalify/rule.rb', line 41

def init(hash, path="", rule_table={})
   unless hash.is_a?(Hash)
      #* key=:schema_notmap  msg="schema definition is not a mapping."
      raise Kwalify.schema_error(:schema_notmap, nil, (!path || path.empty? ? "/" : path), nil)
   end
   rule = self
   rule_table[hash.__id__] = rule
   ## 'type:' entry
   _init_type_value(hash['type'], rule, path)
   ## other entries
   hash.each do |key, val|
      code = key.intern
      curr_path = "#{path}/#{key}"
      case code
      #when "id"
      #   @id       = val
      when :type      ;  # done
      when :name      ;  _init_name_value(    val, rule, path)
      when :desc      ;  _init_desc_value(    val, rule, path)
      when :required  ;  _init_required_value(val, rule, path)
      when :pattern   ;  _init_pattern_value( val, rule, path)
      when :enum      ;  _init_enum_value(    val, rule, path)
      when :assert    ;  _init_assert_value(  val, rule, path)
      when :range     ;  _init_range_value(   val, rule, path)
      when :length    ;  _init_length_value(  val, rule, path)
      when :ident     ;  _init_ident_value(   val, rule, path)
      when :unique    ;  _init_unique_value(  val, rule, path)
      when :sequence  ;  _init_sequence_value(val, rule, path, rule_table)
      when :mapping   ;  _init_mapping_value( val, rule, path, rule_table)
      when :classname ;  _init_classname_value(val, rule, path)
      else
         #* key=:key_unknown  msg="unknown key."
         raise schema_error(:key_unknown, rule, curr_path, "#{key}:")
      end
   end
   check_confliction(hash, rule, path)
   return self
end