module Neo4j::Shared::TypecastedAttributes
TypecastedAttributes
allows types to be declared for your attributes
Types are declared by passing the :type option to the attribute class method. After a type is declared, attribute readers will convert any assigned attribute value to the declared type. If the assigned value cannot be cast, nil will be returned instead. You can access the original assigned value using the before_type_cast methods.
See {Typecasting} for the currently supported types.
@example Usage
class Person include Neo4j::Shared::TypecastedAttributes attribute :age, :type => Integer end person = Person.new person.age = "29" person.age #=> 29 person.age_before_type_cast #=> "29"
Originally part of ActiveAttr, github.com/cgriego/active_attr
Public Instance Methods
Read the raw attribute value
@example Reading a raw age value
person.age = "29" person.attribute_before_type_cast(:age) #=> "29"
@param [String, Symbol, to_s] name Attribute name
@return [Object, nil] The attribute value before typecasting
# File lib/neo4j/shared/typecasted_attributes.rb 41 def attribute_before_type_cast(name) 42 @attributes ||= {} 43 @attributes[name.to_s] 44 end
Private Instance Methods
Calculates an attribute type
@private
# File lib/neo4j/shared/typecasted_attributes.rb 60 def _attribute_type(attribute_name) 61 self.class._attribute_type(attribute_name) 62 end
Resolve an attribute typecaster
@private
# File lib/neo4j/shared/typecasted_attributes.rb 67 def _attribute_typecaster(attribute_name) 68 type = _attribute_type(attribute_name) 69 caster = self.class.attributes[attribute_name].typecaster || Neo4j::Shared::TypeConverters.typecaster_for(type) 70 caster || fail(Neo4j::UnknownTypeConverterError, "Unable to cast to type #{type}") 71 end
Reads the attribute and typecasts the result
Neo4j::Shared::Attributes#attribute
# File lib/neo4j/shared/typecasted_attributes.rb 49 def attribute(name) 50 typecast_attribute(_attribute_typecaster(name), super) 51 end
# File lib/neo4j/shared/typecasted_attributes.rb 53 def typecast_attribute(typecaster, value) 54 self.class.typecast_attribute(typecaster, value) 55 end