Class: Strobe::Key
- Inherits:
-
Object
- Object
- Strobe::Key
- Defined in:
- lib/strobe/key.rb
Instance Attribute Summary collapse
-
#property ⇒ Object
readonly
Returns the value of attribute property.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(property, type) ⇒ Key
constructor
A new instance of Key.
- #key_for(obj) ⇒ Object
- #typecast(val) ⇒ Object
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
#property ⇒ Object (readonly)
Returns the value of attribute property.
3 4 5 |
# File 'lib/strobe/key.rb', line 3 def property @property end |
#type ⇒ Object (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 |