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

Class Method Details

.booleanObject



97
98
99
# File 'lib/lycra/types.rb', line 97

def self.boolean
  Boolean
end

.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

.dateObject



81
82
83
# File 'lib/lycra/types.rb', line 81

def self.date
  Date
end

.floatObject



73
74
75
# File 'lib/lycra/types.rb', line 73

def self.float
  Float
end

.hashObject



110
111
112
# File 'lib/lycra/types.rb', line 110

def self.hash
  Hash
end

.integerObject



65
66
67
# File 'lib/lycra/types.rb', line 65

def self.integer
  Integer
end

.nestedObject



134
135
136
# File 'lib/lycra/types.rb', line 134

def self.nested
  Nested
end

.objectObject



123
124
125
# File 'lib/lycra/types.rb', line 123

def self.object
  Object
end

.textObject



57
58
59
# File 'lib/lycra/types.rb', line 57

def self.text
  Text
end