Class: VacmanController::Token::Properties

Inherits:
Object
  • Object
show all
Defined in:
lib/vacman_controller/token/properties.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Properties

Returns a new instance of Properties.



14
15
16
# File 'lib/vacman_controller/token/properties.rb', line 14

def initialize(token)
  @token = token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Exposes a getter and setter method for each property name. Delegates error handling for invalid properties to [] and []=.



235
236
237
238
239
240
241
242
243
# File 'lib/vacman_controller/token/properties.rb', line 235

def method_missing(name, *args, &block)
  prop, setter = name.to_s.match(/\A(.+?)(=)?\Z/).values_at(1, 2)

  if setter
    self[prop] = args.first
  else
    self[prop]
  end
end

Class Method Details

.namesObject

Gets the available token property names



8
9
10
# File 'lib/vacman_controller/token/properties.rb', line 8

def names
  @_names ||= VacmanController::LowLevel.token_property_names.freeze
end

Instance Method Details

#[](name) ⇒ Object

Get a Token property

Parameters:

property

the property name. See Token::Properties.names



39
40
41
42
43
44
# File 'lib/vacman_controller/token/properties.rb', line 39

def [](name)
  name  = name.to_s
  value = VacmanController::LowLevel.get_token_property(@token.to_h, name)

  read_cast(name, value)
end

#[]=(name, value) ⇒ Object

Set a Token property

Parameters:

property

the property name. See Token.property_names

value

the property value. The AAL2 library accepts only values convertible to integer. For symmetry with [], boolean values are converted to the appropriate integer.

see

write_cast!



62
63
64
65
66
67
# File 'lib/vacman_controller/token/properties.rb', line 62

def []=(name, value)
  name  = name.to_s
  value = write_cast!(name, value)

  VacmanController::LowLevel.set_token_property(@token.to_h, name, value)
end

#allObject Also known as: to_h



24
25
26
27
28
# File 'lib/vacman_controller/token/properties.rb', line 24

def all
  self.class.names.inject({}) do |h, name|
    h.update(name => (self[name] rescue "ERROR: #$!"))
  end
end

#inspectObject



19
20
21
# File 'lib/vacman_controller/token/properties.rb', line 19

def inspect
  all.inspect
end