Class: Lycra::Types::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/lycra/types.rb

Direct Known Subclasses

Boolean, Date, Float, Hash, Integer, Nested, Object, Text

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Type

Returns a new instance of Type.



35
36
37
# File 'lib/lycra/types.rb', line 35

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/lycra/types.rb', line 4

def value
  @value
end

Class Method Details

.klasses(*klasses) ⇒ Object



7
8
9
10
# File 'lib/lycra/types.rb', line 7

def klasses(*klasses)
  @_klasses = klasses unless klasses.empty?
  @_klasses || []
end

.to_sObject



16
17
18
19
# File 'lib/lycra/types.rb', line 16

def type(type=nil)
  @_type = type if type
  @_type ||= self.name.demodulize.underscore
end

.transform(value) ⇒ Object



30
31
32
# File 'lib/lycra/types.rb', line 30

def transform(value)
  value # noop by default
end

.type(type = nil) ⇒ Object



12
13
14
15
# File 'lib/lycra/types.rb', line 12

def type(type=nil)
  @_type = type if type
  @_type ||= self.name.demodulize.underscore
end

.valid?(value, required = false, nested = false) ⇒ Boolean

Returns:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lycra/types.rb', line 18

def 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
    klasses.any? { |klass| value.all? { |val| val.is_a?(klass) } }
  else
    klasses.any? { |klass| value.is_a?(klass) }
  end
end

Instance Method Details

#transformObject



48
49
50
# File 'lib/lycra/types.rb', line 48

def transform
  @transformed ||= self.class.transform(@value)
end

#type(type = nil) ⇒ Object



39
40
41
42
# File 'lib/lycra/types.rb', line 39

def type(type=nil)
  @type = type if type
  @type ||= self.class.type
end

#valid?(required = false, nested = false) ⇒ Boolean

Returns:



44
45
46
# File 'lib/lycra/types.rb', line 44

def valid?(required=false, nested=false)
  @valid ||= self.class.valid?(@value, required, nested)
end