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.



401
402
403
404
# File 'lib/bindata/dsl.rb', line 401

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)


450
451
452
453
454
455
456
457
458
459
# File 'lib/bindata/dsl.rb', line 450

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)


465
466
467
# File 'lib/bindata/dsl.rb', line 465

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

#ensure_valid_name(name) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/bindata/dsl.rb', line 422

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



477
478
479
# File 'lib/bindata/dsl.rb', line 477

def fields
  @dsl_parser.fields
end

#malformed_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


461
462
463
# File 'lib/bindata/dsl.rb', line 461

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

#must_have_a_name_failed?(name) ⇒ Boolean

Returns:

  • (Boolean)


446
447
448
# File 'lib/bindata/dsl.rb', line 446

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

#must_not_have_a_name_failed?(name) ⇒ Boolean

Returns:

  • (Boolean)


442
443
444
# File 'lib/bindata/dsl.rb', line 442

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

#name_is_reserved?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#name_shadows_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#option?(opt) ⇒ Boolean

Returns:

  • (Boolean)


481
482
483
# File 'lib/bindata/dsl.rb', line 481

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

#validate_field(name) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/bindata/dsl.rb', line 406

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