Class: Strobe::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property, type) ⇒ Key

Returns a new instance of Key.



5
6
7
8
# File 'lib/strobe/key.rb', line 5

def initialize(property, type)
  @property = property.to_s
  @type     = type
end

Instance Attribute Details

#propertyObject (readonly)

Returns the value of attribute property.



3
4
5
# File 'lib/strobe/key.rb', line 3

def property
  @property
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/strobe/key.rb', line 3

def type
  @type
end

Instance Method Details

#key_for(obj) ⇒ Object



10
11
12
# File 'lib/strobe/key.rb', line 10

def key_for(obj)
  typecast(obj[property])
end

#typecast(val) ⇒ Object



14
15
16
17
18
19
# File 'lib/strobe/key.rb', line 14

def typecast(val)
  return if val.nil?
  return val.to_s if type == String
  return val.to_i if type == Integer
  raise "Unknown key type `#{type}`"
end