Module: Lycra::Types
- Defined in:
- lib/lycra/types.rb
Defined Under Namespace
Classes: Boolean, Date, Float, Hash, Integer, Nested, Object, Text, Type
Class Method Summary collapse
- .boolean ⇒ Object
- .custom(type) ⇒ Object
- .date ⇒ Object
- .float ⇒ Object
- .hash ⇒ Object
- .integer ⇒ Object
- .nested ⇒ Object
- .object ⇒ Object
- .text ⇒ Object
Class Method Details
.custom(type) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/lycra/types.rb', line 138 def self.custom(type) Class.new(Type) do self.type(type) def self.valid?(value, required=false, nested=false) return false if required && value.nil? return true if value.nil? return false if nested && !value.is_a?(Enumerable) return true if nested && value.empty? if nested value.all? { |val| val.is_a?(type) || val.is_a?(type.subject_type) } else value.is_a?(type) || value.is_a?(type.subject_type) end end def self.transform(value) return value.resolve! if value.is_a?(type) if value.is_a?(Enumerable) value.map { |val| type.new(val).resolve! } else type.new(value).resolve! end end end end |