Class: EasyUpnp::ArgumentValidator::TypeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_upnp/control_point/argument_validator.rb

Constant Summary collapse

RUBY_TYPE_TO_UPNP_TYPE =

Valid UPnP types for each ruby class

{
  Array: %w{list},
  Float: %w{r4 r8 number fixed.14.4 float},
  Integer: %w{ui1 ui2 ui4 i1 i2 i4 int},
  String: %w{char string bin.base64 bin.hex uri uuid},
  TrueClass: %w{bool boolean},
  FalseClass: %w{bool boolean},
  DateTime: %w{date dateTime dateTime.tz time time.tz},
  Time: %w{date dateTime dateTime.tz time time.tz},
  Date: %w{date dateTime time}
}
UPNP_TYPE_VALID_CLASSES =

Inversion of RUBY_TYPE_TO_UPNP_TYPE.

Hash[
        RUBY_TYPE_TO_UPNP_TYPE.map { |k,v|
k = Kernel.const_get(k)
v.map { |x| [x, k] }
        }.reduce({}) { |a,i|
Hash[i]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeValidator

Returns a new instance of TypeValidator.

Raises:

  • (ArgumentError)


85
86
87
88
# File 'lib/easy_upnp/control_point/argument_validator.rb', line 85

def initialize(type)
  @valid_classes = UPNP_TYPE_VALID_CLASSES[type]
  raise ArgumentError, "Unrecognized UPnP type: #{type}" if @valid_classes.nil?
end

Instance Attribute Details

#valid_classesObject (readonly)

Returns the value of attribute valid_classes.



83
84
85
# File 'lib/easy_upnp/control_point/argument_validator.rb', line 83

def valid_classes
  @valid_classes
end

Instance Method Details

#validate(value) ⇒ Object



90
91
92
93
94
# File 'lib/easy_upnp/control_point/argument_validator.rb', line 90

def validate(value)
  if !@valid_classes.any? { |x| value.is_a?(x) }
    raise ArgumentError, "#{value} is the wrong type. Should be one of: #{@valid_classes.inspect}"
  end
end