Module: ActiveGraph::Shared::Typecaster
- Defined in:
- lib/active_graph/shared/typecaster.rb
Overview
This module provides a convenient way of registering a custom Typecasting class. Custom Typecasters all follow a simple pattern.
EXAMPLE:
- .. code-block
-
ruby
class RangeConverter
class << self
def primitive_type
String
end
def convert_type
Range
end
def to_db(value)
value.to_s
end
def to_ruby(value)
ends = value.to_s.split('..').map { |d| Integer(d) }
ends[0]..ends[1]
end
alias_method :call, :to_ruby
end
include ActiveGraph::Shared::Typecaster
end
This would allow you to use `property :my_prop, type: Range` in a model. Each method and the `alias_method` call is required. Make sure the module inclusion happens at the end of the file.
`primitive_type` is used to fool ActiveAttr's type converters, which only recognize a few basic Ruby classes.
`convert_type` must match the constant given to the `type` option.
`to_db` provides logic required to transform your value into the class defined by `primitive_type`
`to_ruby` provides logic to transform the DB-provided value back into the class expected by code using the property. In other words, it should match the `convert_type`.
Note that `alias_method` is used to make `to_ruby` respond to `call`. This is to provide compatibility with ActiveAttr.
Class Method Summary collapse
Class Method Details
.included(other) ⇒ Object
48 49 50 |
# File 'lib/active_graph/shared/typecaster.rb', line 48 def self.included(other) ActiveGraph::Shared::TypeConverters.register_converter(other) end |