Class: VectorEmbed::Maker::Date

Inherits:
VectorEmbed::Maker show all
Defined in:
lib/vector_embed/maker/date.rb

Constant Summary collapse

BASE =
::Date.new(2000,1,1)
ISO_8601_DATE =
/\A\d\d\d\d-\d\d-\d\d\z/
BLANK =
/\A\s*\z/

Instance Attribute Summary

Attributes inherited from VectorEmbed::Maker

#cardinality, #k, #options, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VectorEmbed::Maker

#initialize, #pairs, pick

Constructor Details

This class inherits a constructor from VectorEmbed::Maker

Class Method Details

.want?(v, parent) ⇒ Boolean

Returns:



8
9
10
11
12
13
14
15
# File 'lib/vector_embed/maker/date.rb', line 8

def want?(v, parent)
  case v
  when ::Date
    true
  when ::String
    v =~ ISO_8601_DATE
  end
end

Instance Method Details

#value(v) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vector_embed/maker/date.rb', line 22

def value(v)
  date = case v
  when ::NilClass
    nil
  when ::String
    if v !~ BLANK
      ::Date.parse v
    end
  when ::Date
    v
  else
    raise "Can't embed #{v.inspect} in date feature #{k.inspect}"
  end
  if date
    num = (date - BASE).to_i
    if num.nonzero?
      num
    end
  end
end