Class: Rubyang::Database::DataTree::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyang/database/data_tree.rb

Direct Known Subclasses

Anyxml, InteriorNode, LeafNode, ListNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, schema_tree, schema, db_mode, ctx_mode) ⇒ Node

Returns a new instance of Node.



25
26
27
28
29
30
31
32
33
# File 'lib/rubyang/database/data_tree.rb', line 25

def initialize parent, schema_tree, schema, db_mode, ctx_mode
  @logger = Logger.new(self.class.name)
  @parent = parent
  @schema_tree = schema_tree
  @schema = schema
  @db_mode = db_mode
  @ctx_mode = ctx_mode
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



24
25
26
# File 'lib/rubyang/database/data_tree.rb', line 24

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



24
25
26
# File 'lib/rubyang/database/data_tree.rb', line 24

def parent
  @parent
end

#schemaObject (readonly)

Returns the value of attribute schema.



24
25
26
# File 'lib/rubyang/database/data_tree.rb', line 24

def schema
  @schema
end

#schema_treeObject (readonly)

Returns the value of attribute schema_tree.



24
25
26
# File 'lib/rubyang/database/data_tree.rb', line 24

def schema_tree
  @schema_tree
end

Instance Method Details

#evaluate_max_elementsObject

min-elements start



158
159
160
161
162
163
164
165
# File 'lib/rubyang/database/data_tree.rb', line 158

def evaluate_max_elements
  if @schema.max_elements.size > 0
    @logger.debug { "#{self.class}#evaluate_max_elements: @schema.max_elements.first.arg: #{@schema.max_elements.first.arg}" }
    if @children.size <= @schema.max_elements.first.arg.to_i then true else false end
  else
    true
  end
end

#evaluate_min_elementsObject

min-elements start



147
148
149
150
151
152
153
154
# File 'lib/rubyang/database/data_tree.rb', line 147

def evaluate_min_elements
  if @schema.min_elements.size > 0
    @logger.debug { "#{self.class}#evaluate_min_elements: @schema.min_elements.first.arg: #{@schema.min_elements.first.arg}" }
    if @children.size >= @schema.min_elements.first.arg.to_i then true else false end
  else
    true
  end
end

#evaluate_mustsObject

must start



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rubyang/database/data_tree.rb', line 129

def evaluate_musts
  @schema.musts.inject( Rubyang::Xpath::BasicType::Boolean.new true ){ |r, w|
    @logger.debug { 'evaluate musts:' }
    @logger.debug { 'r:' }
    @logger.debug { r.value }
    @logger.debug { 'w:' }
    @logger.debug { w.arg }
    must = r.and self.evaluate_xpath( w.xpath, self )
    @logger.debug { 'must:' }
    require 'yaml'
    @logger.debug { must.to_yaml }
    @logger.debug { 'evaluate musts done' }
    r.and self.evaluate_xpath( w.xpath, self )
  }
end

#evaluate_whensObject

when start



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubyang/database/data_tree.rb', line 112

def evaluate_whens
  @schema.whens.inject( Rubyang::Xpath::BasicType::Boolean.new true ){ |r, w|
    @logger.debug { 'evaluate whens:' }
    @logger.debug { 'r:' }
    @logger.debug { r.value }
    @logger.debug { 'w:' }
    @logger.debug { w.arg }
    _when = r.and self.evaluate_xpath( w.xpath, self )
    @logger.debug { '_when:' }
    @logger.debug { _when.to_yaml }
    @logger.debug { 'evaluate whens done' }
    r.and self.evaluate_xpath( w.xpath, self )
  }
end

#evaluate_xpath(xpath, current = self) ⇒ Object

end



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rubyang/database/data_tree.rb', line 168

def evaluate_xpath xpath, current=self
  @logger.debug { 'in evaluate_xpath:' }
  @logger.debug { 'xpath:' }
  @logger.debug { xpath.to_yaml }
  @logger.debug { 'self:' }
  @logger.debug { self.class }
  @logger.debug { self.schema.arg }
  @logger.debug { self.schema.value rescue '' }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  @logger.debug { current.schema.arg }
  @logger.debug { current.schema.value rescue '' }
  evaluate_xpath_expr xpath, current
end

#evaluate_xpath_axis(location_step, current) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rubyang/database/data_tree.rb', line 219

def evaluate_xpath_axis location_step, current
  @logger.debug { 'in evaluate_xpath_axis:' }
  @logger.debug { 'location_step:' }
  @logger.debug { location_step.to_yaml }
  @logger.debug { 'self:' }
  @logger.debug { self.class }
  @logger.debug { self.schema.arg }
  @logger.debug { self.schema.value rescue '' }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  @logger.debug { current.schema.arg }
  @logger.debug { current.schema.value rescue '' }
  case location_step.axis.name
  when Rubyang::Xpath::Axis::SELF
    Rubyang::Xpath::BasicType::NodeSet.new [self]
  when Rubyang::Xpath::Axis::PARENT
    Rubyang::Xpath::BasicType::NodeSet.new [@parent]
  when Rubyang::Xpath::Axis::CHILD
    Rubyang::Xpath::BasicType::NodeSet.new @children.inject([]){ |cs, c|
      cs + case c
      when Rubyang::Database::DataTree::ListElement
        c.children
      else
        [c]
      end
    }
  else
    raise "location_step.axis.name: #{location_step.axis.name} NOT implemented"
  end
end

#evaluate_xpath_expr(expr, current = self) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/rubyang/database/data_tree.rb', line 312

def evaluate_xpath_expr expr, current=self
  case expr
  when Rubyang::Xpath::Expr
    @logger.debug { "in Expr" }
    @logger.debug { "op: #{expr.op}" }
    op = expr.op
    op_result = self.evaluate_xpath_expr( op, current )
  when Rubyang::Xpath::OrExpr
    @logger.debug { "in OrExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      if op1_result.class == Rubyang::Xpath::BasicType::NodeSet && op2_result.class == Rubyang::Xpath::BasicType::NodeSet
        if op1_result.empty? && op2_result.empty?
          Rubyang::Xpath::BasicType::Boolean.new false
        else
          Rubyang::Xpath::BasicType::Boolean.new true
        end
      else
        Rubyang::Xpath::BasicType::Boolean.new true
      end
    end
  when Rubyang::Xpath::AndExpr
    @logger.debug { "in AndExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      if op1_result.class == Rubyang::Xpath::BasicType::NodeSet
        Rubyang::Xpath::BasicType::Boolean.new false if op1_result.empty?
      elsif op2_result.class == Rubyang::Xpath::BasicType::NodeSet
        Rubyang::Xpath::BasicType::Boolean.new false if op2_result.empty?
      else
        Rubyang::Xpath::BasicType::Boolean.new true
      end
    end
  when Rubyang::Xpath::EqualityExpr
    @logger.debug { "in EqualityExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      @logger.debug { "in EqualityExpr else:" }
      @logger.debug { "op1_result: #{op1_result.to_yaml}" }
      @logger.debug { "op2_result: #{op2_result.to_yaml}" }
      if op1_result.class == Rubyang::Xpath::BasicType::NodeSet && op2_result.class == Rubyang::Xpath::BasicType::String
        case operator
        when /^\=$/
          #op1_result.select{ |a| op2_result.map{ |b| b.value }.include? a.value }.map{ |c| c.parent }
          op1_result == op2_result
        when /^\!\=$/
          raise "Equality Expr: '!=' not implemented"
        else
          raise "Equality Expr: other than '=' and '!=' not implemented"
        end
      elsif op1_result.class == Rubyang::Xpath::BasicType::String && op2_result.class == Rubyang::Xpath::BasicType::NodeSet
        case operator
        when /^\=$/
          op2_result.select{ |a| op1_result.map{ |b| b.value }.include? a.value }.map{ |c| c.parent }
        when /^\!\=$/
          raise "Equality Expr: '!=' not implemented"
        else
          raise "Equality Expr: other than '=' and '!=' not implemented"
        end
      elsif op1_result.class == Rubyang::Xpath::BasicType::Number && op2_result.class == Rubyang::Xpath::BasicType::Number
        case operator
        when /^\=$/
          op1_result == op2_result
        when /^\!\=$/
          op1_result != op2_result
        else
          raise "Equality Expr: other than '=' and '!=' not implemented"
        end
      elsif op1_result.class == Rubyang::Xpath::BasicType::String && op2_result.class == Rubyang::Xpath::BasicType::String
        case operator
        when /^\=$/
          op1_result == op2_result
        when /^\!\=$/
          op1_result != op2_result
        else
          raise "Equality Expr: other than '=' and '!=' not implemented"
        end
      elsif op1_result.class == Rubyang::Xpath::BasicType::NodeSet && op2_result.class == Rubyang::Xpath::BasicType::NodeSet
        case operator
        when /^\=$/
          op1_result == op2_result
        when /^\!\=$/
          op1_result != op2_result
        else
          raise "Equality Expr: other than '=' and '!=' not implemented"
        end
      else
        Rubyang::Xpath::BasicType::Boolean.new false
      end
    end
  when Rubyang::Xpath::RelationalExpr
    @logger.debug { "in RelationalExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      if op1_result.class == Rubyang::Xpath::BasicType::Number && op2_result.class == Rubyang::Xpath::BasicType::Number
        case operator
        when /^\>$/
          op1_result > op2_result
        when /^\<$/
          op1_result < op2_result
        when /^\>\=$/
          op1_result >= op2_result
        when /^\<\=$/
          op1_result <= op2_result
        else
          raise "Relational Expr: other than '>', '<', '>=' and '<=' not valid"
        end
      else
        Rubyang::Xpath::BasicType::Boolean.new false
      end
    end
  when Rubyang::Xpath::AdditiveExpr
    @logger.debug { "in AdditiveExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      if op1_result.class == Rubyang::Xpath::BasicType::Number && op2_result.class == Rubyang::Xpath::BasicType::Number
        case operator
        when /^\+$/
          op1_result + op2_result
        when /^\-$/
          op1_result - op2_result
        else
          raise "Additive Expr: other than '+' and '-' not valid"
        end
      else
        Rubyang::Xpath::BasicType::Number.new Float::NAN
      end
    end
  when Rubyang::Xpath::MultiplicativeExpr
    @logger.debug { "in MultiplicativeExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      if op1_result.class == Rubyang::Xpath::BasicType::Number && op2_result.class == Rubyang::Xpath::BasicType::Number
        case operator
        when /^\*$/
          op1_result * op2_result
        when /^\/$/
          op1_result / op2_result
        else
          raise "Multiplicative Expr: other than '*' and '/' not valid"
        end
      else
        Rubyang::Xpath::BasicType::Number.new Float::NAN
      end
    end
  when Rubyang::Xpath::UnaryExpr
    @logger.debug { "in UnaryExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    case operator
    when nil
      op1_result
    when /^\-$/
      case op1_result
      when Rubyang::Xpath::BasicType::Number
        - op1_result
      else
        Rubyang::Xpath::BasicType::Number.new Float::NAN
      end
    else
      raise "Unary Expr: other than '-' not valid"
    end
  when Rubyang::Xpath::UnionExpr
    @logger.debug { "in UnionExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2, current )
      case operator
      when /^\|$/
        raise "Union Expr: '|' not implemented"
      else
        raise "Union Expr: other than '|' not implemented"
      end
    end
  when Rubyang::Xpath::PathExpr
    @logger.debug { "in PathExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    @logger.debug { "operator: #{expr.operator}" }
    op1 = expr.op1
    op2 = expr.op2
    operator = expr.operator
    op1_result = case op1
                 when Rubyang::Xpath::LocationPath
                   self.evaluate_xpath_path( op1, current )
                 when Rubyang::Xpath::FilterExpr
                   op1_result = self.evaluate_xpath_expr( op1, current )
                 else
                   raise "PathExpr: #{op1} not supported"
                 end
    if op2 == nil
      op1_result
    else
      case operator
      when /^\/$/
        case op1_result
        when Rubyang::Database::DataTree::Node
          op1_result.evaluate_xpath_path op2, current
        when Rubyang::Xpath::LocationPath
          self.evaluate_xpath_path Rubyang::Xpath::LocationPath.new( *(op1_result.location_step_sequence + op2.location_step_sequence) ), current
        else
          raise
        end
      when /^\/\/$/
        raise "Path Expr: '//' not implemented"
      else
        raise "Path Expr: other than '/' and '//' not valid"
      end
    end
  when Rubyang::Xpath::FilterExpr
    @logger.debug { "in FilterExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    @logger.debug { "op2: #{expr.op2}" }
    op1 = expr.op1
    op2 = expr.op2
    op1_result = self.evaluate_xpath_expr( op1, current )
    if op2 == nil
      op1_result
    else
      op2_result = self.evaluate_xpath_expr( op2.expr, current )
      Rubyang::Xpath::BasicType::NodeSet.new
    end
  when Rubyang::Xpath::PrimaryExpr
    @logger.debug { "in PrimaryExpr" }
    @logger.debug { "op1: #{expr.op1}" }
    op1 = expr.op1
    case op1
    when Rubyang::Xpath::VariableReference
      raise "Primary Expr: '#{op1}' not implemented"
    when Rubyang::Xpath::Expr
      op1_result = self.evaluate_xpath_expr( op1, current )
    when Rubyang::Xpath::Literal
      Rubyang::Xpath::BasicType::String.new op1.value
    when Rubyang::Xpath::Number
      Rubyang::Xpath::BasicType::Number.new op1.value
    when Rubyang::Xpath::FunctionCall
      op1_result = self.evaluate_xpath_expr( op1, current )
    else
      raise "Primary Expr: '#{op1}' not valid"
    end
  when Rubyang::Xpath::FunctionCall
    @logger.debug { "in FunctionCall" }
    @logger.debug { "name: #{expr.name}" }
    @logger.debug { "args: #{expr.args}" }
    name = expr.name
    case name
    when Rubyang::Xpath::FunctionCall::CURRENT
      current
    else
      raise "FunctionCall: #{name} not implemented"
    end
  else
    raise "Unrecognized Expr: #{expr}"
  end
end

#evaluate_xpath_node_test(location_step, current) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rubyang/database/data_tree.rb', line 250

def evaluate_xpath_node_test location_step, current
  @logger.debug { 'in node_test' }
  @logger.debug { self.class }
  @logger.debug { self.schema.arg }
  @logger.debug { self.value rescue '' }
  case location_step.node_test.node_test_type
  when Rubyang::Xpath::NodeTest::NodeTestType::NAME_TEST
    if "/" == location_step.node_test.node_test
      [self.root]
    elsif self.schema.model.arg == location_step.node_test.node_test
      case self
      when Rubyang::Database::DataTree::List
        self.children
      else
        [self]
      end
    else
      []
    end
  when Rubyang::Xpath::NodeTest::NodeTestType::NODE_TYPE
    case location_step.node_test.node_test
    when Rubyang::Xpath::NodeTest::NodeType::COMMENT
      raise "node-type: comment is not implemented"
    when Rubyang::Xpath::NodeTest::NodeType::TEXT
      raise "node-type: text is not implemented"
    when Rubyang::Xpath::NodeTest::NodeType::NODE
      [self]
    else
      raise "node-type not match to comment or text or node"
    end
  when Rubyang::Xpath::NodeTest::NodeTestType::PROCESSING_INSTRUCTION
    raise "processing-instruction is not implemented"
  else
    raise ""
  end
end

#evaluate_xpath_path(location_path, current) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rubyang/database/data_tree.rb', line 183

def evaluate_xpath_path location_path, current
  @logger.debug { 'in evaluate_xpath_path:' }
  @logger.debug { 'location_path:' }
  @logger.debug { location_path.to_yaml }
  @logger.debug { 'self:' }
  @logger.debug { self.class }
  @logger.debug { self.schema.arg }
  @logger.debug { self.schema.value rescue '' }
  @logger.debug { 'current:' }
  @logger.debug { current.class }
  @logger.debug { current.schema.arg }
  @logger.debug { current.schema.value rescue '' }
  first_location_step = location_path.location_step_sequence.first
  @logger.debug { 'first_location_step:' }
  @logger.debug { first_location_step.to_yaml }
  candidates_by_axis = self.evaluate_xpath_axis( first_location_step, current )
  @logger.debug { 'candidates_by_axis:' }
  @logger.debug { candidates_by_axis.to_yaml }
  candidates_by_node_test = Rubyang::Xpath::BasicType::NodeSet.new candidates_by_axis.value.inject([]){ |cs, c| cs + c.evaluate_xpath_node_test( first_location_step, current ) }
  @logger.debug { 'candidates_by_node_test:' }
  @logger.debug { candidates_by_node_test.to_yaml }
  candidates_by_predicates = Rubyang::Xpath::BasicType::NodeSet.new candidates_by_node_test.value.inject([]){ |cs, c| cs + c.evaluate_xpath_predicates( first_location_step, current ) }
  @logger.debug { 'candidates_by_predicates:' }
  @logger.debug { candidates_by_predicates.to_yaml }
  if location_path.location_step_sequence[1..-1].size == 0
    candidates_by_predicates
  else
    Rubyang::Xpath::BasicType::NodeSet.new candidates_by_predicates.value.inject([]){ |cs, c|
      following_location_path = Rubyang::Xpath::LocationPath.new *(location_path.location_step_sequence[1..-1])
      @logger.debug { 'following_location_path:' }
      @logger.debug { following_location_path.to_yaml }
      cs + c.evaluate_xpath_path( following_location_path, current ).value
    }
  end
end

#evaluate_xpath_predicates(location_step, current) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rubyang/database/data_tree.rb', line 287

def evaluate_xpath_predicates location_step, current
  case location_step.predicates.size
  when 0
    [self]
  else
    location_step.predicates.inject([self]){ |cs, predicate|
      if cs.size > 0
        result = cs[0].evaluate_xpath_expr predicate.expr, current
        case result
        when Rubyang::Xpath::BasicType::NodeSet
          raise
        when Rubyang::Xpath::BasicType::Boolean
          if result.value == true then cs else [] end
        when Rubyang::Xpath::BasicType::Number
          raise
        when Rubyang::Xpath::BasicType::String
          raise
        end
      else
        []
      end
    }
  end
end

#load_merge_xml_recursive(doc_xml) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubyang/database/data_tree.rb', line 85

def load_merge_xml_recursive doc_xml
  doc_xml.each_element{ |e|
    child = edit( e.name )
    unless e.has_elements?
      if e.has_text?
        classes_have_set = [
          Rubyang::Database::DataTree::Leaf,
          Rubyang::Database::DataTree::LeafList,
        ]
        if classes_have_set.include?(child.class)
          child.set e.text
        end
      end
    end
    child.load_merge_xml_recursive e
  }
end

#rootObject



102
103
104
105
106
107
108
109
# File 'lib/rubyang/database/data_tree.rb', line 102

def root
  case @parent
  when Rubyang::Database::DataTree::Root
    @parent
  else
    @parent.root
  end
end

#to_json(pretty: false) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/rubyang/database/data_tree.rb', line 57

def to_json pretty: false
  hash = Hash.new
  self.to_json_recursive hash
  if pretty
    JSON.pretty_generate( hash )
  else
    JSON.generate( hash )
  end
end

#to_s(parent = true) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubyang/database/data_tree.rb', line 34

def to_s parent=true
  head, vars, tail = "#<#{self.class.to_s}:0x#{(self.object_id << 1).to_s(16).rjust(14,'0')} ", Array.new, ">"
  if parent
    vars.push "@parent=#{@parent.to_s(false)}"
    vars.push "@schema_tree=#{@schema_tree.to_s(false)}"
    vars.push "@schema=#{@schema.to_s(true)}"
    vars.push "@children=#{@children.to_s}"
  end
  head + vars.join(', ') + tail
end

#to_xml(pretty: false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubyang/database/data_tree.rb', line 44

def to_xml pretty: false
  doc = REXML::Document.new
  self.to_xml_recursive doc, ''
  if pretty
    pretty_formatter = REXML::Formatters::Pretty.new( 2 )
    pretty_formatter.compact = true
    output = ''
    pretty_formatter.write( doc, output )
    output
  else
    doc.to_s
  end
end

#valid?(current = true) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubyang/database/data_tree.rb', line 66

def valid? current=true
  result = if current
             self.root.valid?
           else
             case self
             when Rubyang::Database::DataTree::Container
               @children.inject(self.evaluate_musts){ |r, c|
                 r.and c.valid?( false )
               }
             when Rubyang::Database::DataTree::LeafList, Rubyang::Database::DataTree::List
               tmp = Rubyang::Xpath::BasicType::Boolean.new( self.evaluate_min_elements )
               tmp.and Rubyang::Xpath::BasicType::Boolean.new( self.evaluate_max_elements )
             else
               Rubyang::Xpath::BasicType::Boolean.new true
             end
           end
  @logger.debug { "#{self.class}#valid?: return: #{result} #{result.value}" }
  result
end