Class: DatasetExplorer::ValueEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/dataset_explorer/value_evaluator.rb

Defined Under Namespace

Classes: BooleanEvaluator, DateEvaluator, FloatEvaluator, IntegerEvaluator, StringEvaluator, TimeEvaluator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name) ⇒ ValueEvaluator

Returns a new instance of ValueEvaluator.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dataset_explorer/value_evaluator.rb', line 12

def initialize(field_name)
  @field_name = field_name
  @null = true
  @evaluators = {
    'date' => DateEvaluator.new,
    'time' => TimeEvaluator.new,
    'string' => StringEvaluator.new,
    'float' => FloatEvaluator.new,
    'integer' => IntegerEvaluator.new,
    'boolean' => BooleanEvaluator.new
  }
  @min_length = nil
  @max_length = nil
end

Instance Attribute Details

#max_lengthObject (readonly)

Returns the value of attribute max_length.



10
11
12
# File 'lib/dataset_explorer/value_evaluator.rb', line 10

def max_length
  @max_length
end

#min_lengthObject (readonly)

Returns the value of attribute min_length.



9
10
11
# File 'lib/dataset_explorer/value_evaluator.rb', line 9

def min_length
  @min_length
end

Instance Method Details

#describeObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dataset_explorer/value_evaluator.rb', line 46

def describe
  parts = ["Possible types: [#{types.join(', ')}]"]
  if @null
    parts << 'NULL'
  end

  if min_length
    parts << "Min/max Length: #{min_length}/#{max_length}"
  end

  parts.join(', ')
end

#evaluate(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dataset_explorer/value_evaluator.rb', line 27

def evaluate(value)
  if value.nil?
    @null = true
    return
  end

  evaluate_length(value)

  @evaluators.each do |key, evaluator|
    unless evaluator.accept?(value)
      @evaluators.delete(key)
    end
  end
end

#evaluate_length(value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dataset_explorer/value_evaluator.rb', line 59

def evaluate_length(value)
  unless value.respond_to?(:length)
    return
  end

  length = value.length
  @min_length ||= length
  @max_length ||= length

  if @min_length > length
    @min_length = length
  end

  if @max_length < length
    @max_length = length
  end
end

#typesObject



42
43
44
# File 'lib/dataset_explorer/value_evaluator.rb', line 42

def types
  @evaluators.keys
end