Class: Rx::Type::Core::Str

Inherits:
Rx::Type::Core show all
Defined in:
lib/rx/ruby/Rx.rb

Constant Summary collapse

@@allowed_param =
{ 'type' => true, 'value' => true, 'length' => true }

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rx::Type::Core

#check, core_types, uri

Methods inherited from Rx::Type

#assert_valid_params, #uri

Constructor Details

#initialize(param, rx) ⇒ Str

Returns a new instance of Str.



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/rx/ruby/Rx.rb', line 585

def initialize(param, rx)
  super

  if param['length'] then
    @length_range = Rx::Helper::Range.new( param['length'] )
  end

  if param.has_key?('value') then
    if ! param['value'].instance_of?(String) then
      raise Rx::Exception.new("invalid value parameter for #{uri}")
    end

    @value = param['value']
  end
end

Class Method Details

.subnameObject



581
# File 'lib/rx/ruby/Rx.rb', line 581

def subname; return 'str'; end

Instance Method Details

#allowed_param?(p) ⇒ Boolean

Returns:

  • (Boolean)


583
# File 'lib/rx/ruby/Rx.rb', line 583

def allowed_param?(p); return @@allowed_param[p]; end

#check!(value) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/rx/ruby/Rx.rb', line 601

def check!(value)
  unless value.instance_of?(String)
    raise ValidationError.new("expected String got #{value.inspect}", "/str")
  end

  if @length_range
    unless @length_range.check(value.length)
      raise ValidationError.new("expected string with #{@length_range} characters, got #{value.length}", "/str")
    end
  end

  if @value and value != @value
    raise ValidationError.new("expected #{@value.inspect} got #{value.inspect}", "/str")
  end
  return true
end