Parent

Files

Class/Module Index [+]

Quicksearch

BinData::DSLMixin::DSLFieldValidator

Validates a field defined in a DSLMixin.

Public Class Methods

new(the_class, parser) click to toggle source
# File lib/bindata/dsl.rb, line 370
def initialize(the_class, parser)
  @the_class = the_class
  @dsl_parser = parser
end

Public Instance Methods

all_or_none_names_failed?(name) click to toggle source
# File lib/bindata/dsl.rb, line 419
def all_or_none_names_failed?(name)
  if option?(:all_or_none_fieldnames) and not fields.empty?
    all_names_blank = fields.all_field_names_blank?
    no_names_blank = fields.no_field_names_blank?

    (name != nil and all_names_blank) or (name == nil and no_names_blank)
  else
    false
  end
end
duplicate_name?(name) click to toggle source
# File lib/bindata/dsl.rb, line 434
def duplicate_name?(name)
  fields.has_field_name?(name)
end
ensure_valid_name(name) click to toggle source
# File lib/bindata/dsl.rb, line 391
def ensure_valid_name(name)
  if name and not option?(:fieldnames_are_values)
    if malformed_name?(name)
      raise NameError.new("", name), "field '#{name}' is an illegal fieldname"
    end

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

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

    if name_is_reserved?(name)
      raise NameError.new("", name), "field '#{name}' is a reserved name"
    end
  end
end
fields() click to toggle source
# File lib/bindata/dsl.rb, line 446
def fields
  @dsl_parser.fields
end
malformed_name?(name) click to toggle source
# File lib/bindata/dsl.rb, line 430
def malformed_name?(name)
  /^[a-z_]\w*$/ !~ name.to_s
end
must_have_a_name_failed?(name) click to toggle source
# File lib/bindata/dsl.rb, line 415
def must_have_a_name_failed?(name)
  option?(:mandatory_fieldnames) and name.nil?
end
must_not_have_a_name_failed?(name) click to toggle source
# File lib/bindata/dsl.rb, line 411
def must_not_have_a_name_failed?(name)
  option?(:no_fieldnames) and name != nil
end
name_is_reserved?(name) click to toggle source
# File lib/bindata/dsl.rb, line 442
def name_is_reserved?(name)
  BinData::Struct::RESERVED.include?(name.to_sym)
end
name_shadows_method?(name) click to toggle source
# File lib/bindata/dsl.rb, line 438
def name_shadows_method?(name)
  @the_class.method_defined?(name)
end
option?(opt) click to toggle source
# File lib/bindata/dsl.rb, line 450
def option?(opt)
  @dsl_parser.send(:option?, opt)
end
validate_field(name) click to toggle source
# File lib/bindata/dsl.rb, line 375
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.