Module: NXT::Utils::Accessors

Included in:
Command::Output, Connector::Output::Motor
Defined in:
lib/nxt/utils/accessors.rb

Instance Method Summary collapse

Instance Method Details

#attr_setter(name, options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/nxt/utils/accessors.rb', line 4

def attr_setter(name, options)
  define_method("#{name}=") do |value|
    if options.include?(:is)
      raise TypeError.new('Expected value to be a number') unless duration.is_a?(options[:is])
    end

    if options.include?(:is_key_in) && !options[:is_key_in].include?(value)
      raise TypeError.new("Expected value to be one of: :#{options[:is_key_in].keys.join(', :')}")
    end

    instance_variable_set("@#{name}", value)
    self
  end
end