Class: BinData::DSLMixin::DSLFieldValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/bindata/dsl.rb

Overview

Validates a field defined in a DSLMixin.

Instance Method Summary collapse

Constructor Details

#initialize(the_class, parser) ⇒ DSLFieldValidator

Returns a new instance of DSLFieldValidator.



428
429
430
431
# File 'lib/bindata/dsl.rb', line 428

def initialize(the_class, parser)
  @the_class = the_class
  @dsl_parser = parser
end

Instance Method Details

#all_or_none_names_failed?(name) ⇒ Boolean

Returns:

  • (Boolean)


477
478
479
480
481
482
483
484
485
486
# File 'lib/bindata/dsl.rb', line 477

def all_or_none_names_failed?(name)
  if option?(:all_or_none_fieldnames) && !fields.empty?
    all_names_blank = fields.all_field_names_blank?
    no_names_blank = fields.no_field_names_blank?

    (!name.nil? && all_names_blank) || (name.nil? && no_names_blank)
  else
    false
  end
end

#duplicate_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


492
493
494
# File 'lib/bindata/dsl.rb', line 492

def duplicate_name?(name)
  fields.field_name?(name)
end

#ensure_valid_name(name) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/bindata/dsl.rb', line 449

def ensure_valid_name(name)
  if name && !option?(:fieldnames_are_values)
    if malformed_name?(name)
      raise SyntaxError, "field '#{name}' is an illegal fieldname"
    end

    if duplicate_name?(name)
      raise SyntaxError, "duplicate field '#{name}'"
    end

    if name_shadows_method?(name)
      raise SyntaxError, "field '#{name}' shadows an existing method"
    end

    if name_is_reserved?(name)
      raise SyntaxError, "field '#{name}' is a reserved name"
    end
  end
end

#fieldsObject



504
505
506
# File 'lib/bindata/dsl.rb', line 504

def fields
  @dsl_parser.fields
end

#malformed_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


488
489
490
# File 'lib/bindata/dsl.rb', line 488

def malformed_name?(name)
  !/^[a-z_]\w*$/.match?(name.to_s)
end

#must_have_a_name_failed?(name) ⇒ Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/bindata/dsl.rb', line 473

def must_have_a_name_failed?(name)
  option?(:mandatory_fieldnames) && name.nil?
end

#must_not_have_a_name_failed?(name) ⇒ Boolean

Returns:

  • (Boolean)


469
470
471
# File 'lib/bindata/dsl.rb', line 469

def must_not_have_a_name_failed?(name)
  option?(:no_fieldnames) && !name.nil?
end

#name_is_reserved?(name) ⇒ Boolean

Returns:

  • (Boolean)


500
501
502
# File 'lib/bindata/dsl.rb', line 500

def name_is_reserved?(name)
  BinData::Struct::RESERVED.include?(name.to_sym)
end

#name_shadows_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


496
497
498
# File 'lib/bindata/dsl.rb', line 496

def name_shadows_method?(name)
  @the_class.method_defined?(name)
end

#option?(opt) ⇒ Boolean

Returns:

  • (Boolean)


508
509
510
# File 'lib/bindata/dsl.rb', line 508

def option?(opt)
  @dsl_parser.send(:option?, opt)
end

#validate_field(name) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/bindata/dsl.rb', line 433

def validate_field(name)
  if must_not_have_a_name_failed?(name)
    raise SyntaxError, "field must not have a name"
  end

  if all_or_none_names_failed?(name)
    raise SyntaxError, "fields must either all have names, or none must have names"
  end

  if must_have_a_name_failed?(name)
    raise SyntaxError, "field must have a name"
  end

  ensure_valid_name(name)
end