Module: Wrest::Components::AttributesContainer::Typecaster::ClassMethods
- Defined in:
- lib/wrest/components/attributes_container/typecaster.rb
Instance Method Summary collapse
- #as_base64Binary ⇒ Object
- #as_boolean ⇒ Object
- #as_date ⇒ Object
- #as_datetime ⇒ Object
- #as_decimal ⇒ Object
- #as_float ⇒ Object
- #as_integer ⇒ Object
- #as_symbol ⇒ Object
- #as_yaml ⇒ Object
-
#typecast(cast_map) ⇒ Object
Accepts a set of attribute-name/lambda pairs which are used to typecast string values injected through the constructor.
-
#typecast_map ⇒ Object
:nodoc:.
Instance Method Details
#as_base64Binary ⇒ Object
73 74 75 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 73 def as_base64Binary ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['base64Binary'] end |
#as_boolean ⇒ Object
77 78 79 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 77 def as_boolean ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['boolean'] end |
#as_date ⇒ Object
85 86 87 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 85 def as_date ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['date'] end |
#as_datetime ⇒ Object
89 90 91 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 89 def as_datetime ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['datetime'] end |
#as_decimal ⇒ Object
81 82 83 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 81 def as_decimal ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['decimal'] end |
#as_float ⇒ Object
93 94 95 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 93 def as_float ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['float'] end |
#as_integer ⇒ Object
97 98 99 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 97 def as_integer ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['integer'] end |
#as_symbol ⇒ Object
101 102 103 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 101 def as_symbol ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['symbol'] end |
#as_yaml ⇒ Object
105 106 107 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 105 def as_yaml ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING['yaml'] end |
#typecast(cast_map) ⇒ Object
Accepts a set of attribute-name/lambda pairs which are used to typecast string values injected through the constructor. Typically needed when populating an AttributesContainer
directly from request params. Typecasting kicks in for a given value only if it is a string.
Typecast information is inherited by subclasses; however be aware that explicitly invoking typecast
in a subclass will discard inherited typecast information leaving only the casts defined in the subclass.
Common typecasts such as integer, float, datetime etc. are available through predefined helpers. See TypecastHelpers for a full list.
Example:
class Demon
include Wrest::Components::AttributesContainer
include Wrest::Components::AttributesContainer::Typecaster
typecast :age => as_integer,
:chi => lambda{|chi| Chi.new(chi)}
end
kai_wren = Demon.new('age' => '1500', 'chi' => '1024')
kai_wren.age # => 1500
kai_wren.chi # => #<Chi:0x113af8c @count="1024">
59 60 61 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 59 def typecast(cast_map) @typecast_map = @typecast_map ? @typecast_map.merge(cast_map.symbolize_keys) : cast_map.symbolize_keys end |
#typecast_map ⇒ Object
:nodoc:
63 64 65 66 67 68 69 70 71 |
# File 'lib/wrest/components/attributes_container/typecaster.rb', line 63 def typecast_map #:nodoc: if defined?(@typecast_map) @typecast_map elsif superclass != Object && superclass.respond_to?(:typecast_map) superclass.typecast_map else {} end end |