Class: DrySchemaExtend::SchemaInfoCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/zleb/plugins/schema_compiler.rb

Constant Summary collapse

EMPTY_HASH =
{}
PREDICATE_TO_TYPE =
{
  array?: "array",
  bool?: "bool",
  date?: "date",
  date_time?: "date_time",
  decimal?: "decimal",
  float?: "float",
  hash?: "hash",
  int?: "integer",
  nil?: "nil",
  str?: "string",
  time?: "time",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaInfoCompiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SchemaInfoCompiler.



29
30
31
32
# File 'lib/zleb/plugins/schema_compiler.rb', line 29

def initialize(schema)
  @keys = EMPTY_HASH.dup
  @schema = schema
end

Instance Attribute Details

#keysObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/zleb/plugins/schema_compiler.rb', line 26

def keys
  @keys
end

Instance Method Details

#callObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



40
41
42
# File 'lib/zleb/plugins/schema_compiler.rb', line 40

def call
  visit(@schema.to_ast)
end

#predicate_message(key, name, rest) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/zleb/plugins/schema_compiler.rb', line 146

def predicate_message(key, name, rest)
  result = nil
  if rest.count == 2
    param_name = rest[0][0]
    param_value = rest[0][1]
    
    result = @schema.message_compiler.messages.get_message_for_key(name,{:path=> key, :arg_type => param_value.class})
    if param_value.class == Range
      result = result[:text].gsub("%{#{param_name}_left}", param_value.first.to_s).gsub("%{#{param_name}_right}", param_value.last.to_s)
    else
      result = result[:text].gsub("%{#{param_name}}", param_value.to_s)
    end
  else
    result = @schema.message_compiler.messages.get_message_for_key(name,{:path=> key})
    result = result[:text]
  end
  result
end

#to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/zleb/plugins/schema_compiler.rb', line 35

def to_h
  { children: @keys, type: "hash" }
end

#visit(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



45
46
47
48
# File 'lib/zleb/plugins/schema_compiler.rb', line 45

def visit(node, opts = EMPTY_HASH)
  meth, rest = node
  public_send(:"visit_#{meth}", rest, opts)
end

#visit_and(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
# File 'lib/zleb/plugins/schema_compiler.rb', line 65

def visit_and(node, opts = EMPTY_HASH)
  left, right = node
  
  visit(left, opts)
  visit(right, opts)
end

#visit_each(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
# File 'lib/zleb/plugins/schema_compiler.rb', line 90

def visit_each(node, opts = EMPTY_HASH)
  visit(node, opts.merge(member: true))
end

#visit_implication(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zleb/plugins/schema_compiler.rb', line 73

def visit_implication(node, opts = EMPTY_HASH)
  node.each do |el|
    # p "--------------"
    # p el
    if el[0] == :predicate and el[1][0] == :key?
      visit(el, opts.merge(required: false))
    else
      visit(el, opts)
    end
  end
end

#visit_key(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
# File 'lib/zleb/plugins/schema_compiler.rb', line 95

def visit_key(node, opts = EMPTY_HASH)
  
  name, rest = node
  # puts "visit_key is ### #{name}"
  visit(rest, opts.merge(key: name, required: true, member: false)) # modified by zhanghz
end

#visit_not(node, opts = EMPTY_HASH) ⇒ Object



85
86
87
# File 'lib/zleb/plugins/schema_compiler.rb', line 85

def visit_not(node, opts= EMPTY_HASH)
  visit(node, opts.merge(not: true))
end

#visit_predicate(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/zleb/plugins/schema_compiler.rb', line 103

def visit_predicate(node, opts = EMPTY_HASH)
  name, rest = node
  
  key = opts[:key]
  
  if name.equal?(:key?)
    # p "---key is #{rest[0][1]} #{opts}"
    keys[rest[0][1]] = { required: opts.fetch(:required, true) }
  else
    type = PREDICATE_TO_TYPE[name]
    # p opts
    # p "rest predicate key is #{key} content is #{name}| #{rest}"

    # modified by zhanghz
    if opts[:member] == true
      keys[key][:member] ||= {}
      if type
        if type == 'nil' and opts[:not] == true
          keys[key][:member][:maybe] = type
        else
          keys[key][:member][:type] = type
        end
        keys[key][:type] = "array"
      else
        keys[key][:member][:validations] = [] unless keys[key][:member][:validations]
        keys[key][:member][:validations] << predicate_message(key, name, rest)
      end

    else
      if type
        if type == 'nil' and opts[:not] == true
          keys[key][:maybe] = type
        else
          keys[key][:type] = type
        end
      else
        keys[key][:validations] = [] unless keys[key][:validations]
        keys[key][:validations] << predicate_message(key, name, rest)
      end
    end
  end
end

#visit_set(node, opts = EMPTY_HASH) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zleb/plugins/schema_compiler.rb', line 51

def visit_set(node, opts = EMPTY_HASH)
  target = (key = opts[:key]) ? self.class.new(@schema) : self
  
  node.map { |child| target.visit(child, opts) }
  
  return unless key
  
  target_info = opts[:member] ? { member: target.to_h } : target.to_h
  type = opts[:member] ? "array" : "hash"
  
  keys.update(key => { **keys[key], type: type, **target_info })
end