Module: Clevic::FieldValuer

Included in:
Delegate, Valuer, SimpleFieldValuer, TableIndex
Defined in:
lib/clevic/field_valuer.rb

Overview

To be included in something that responds to entity and field methods. Used for getting values from the entity based on the definitions in the field.

Defined Under Namespace

Classes: Valuer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valuer(field, entity) ⇒ Object



168
169
170
# File 'lib/clevic/field_valuer.rb', line 168

def self.valuer( field, entity )
  Valuer.new(field,entity)
end

Instance Method Details

#attribute_valueObject

fetch the value of the attribute, without following the full path. This will return a related entity for belongs_to or has_one relationships, or a plain value for model attributes



120
121
122
# File 'lib/clevic/field_valuer.rb', line 120

def attribute_value
  entity.send( field.attribute )
end

#attribute_value=(value) ⇒ Object

set the value of the attribute, without following the full path. TODO remove need to constantly recalculate the attribute writer



132
133
134
# File 'lib/clevic/field_valuer.rb', line 132

def attribute_value=( value )
  entity.send( writer, value )
end

#display_valueObject



13
14
15
# File 'lib/clevic/field_valuer.rb', line 13

def display_value
  field.do_format( raw_value ) unless raw_value.nil?
end

#edit_valueObject



17
18
19
# File 'lib/clevic/field_valuer.rb', line 17

def edit_value
  field.do_edit_format( raw_value ) unless raw_value.nil?
end

#edit_value=(value) ⇒ Object

Set the value from an editable text representation of the value



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/clevic/field_valuer.rb', line 23

def edit_value=( value )
  # translate the value from the ui to something that
  # the model will understand
  self.attribute_value =
  case
    # allow flexibility in entering dates. For example
    # 16jun, 16-jun, 16/jun would be accepted here.
    # Previous year is used  if data capture is in January
    # or February, and date.month is aug-dec.
    when [:date,:datetime].include?( field.meta.type ) && value =~ %r{^(\d{1,2})[ /-]?(\w{3})$}
      today = Date.today
      date = Date.parse( "#$1 #$2 #{Time.now.year.to_s}" )

      # change year to last year
      if (1..2).include?( today.month ) and (8..12).include?( date.month )
        date = Date.civil( Date.today.year - 1, date.month, date.day )
      end
      date

    # if a digit only is entered, fetch month and year from
    # previous row
    when [:date,:datetime].include?( field.meta.type ) && value =~ %r{^(\d{1,2})$}
      # year,month,day
      prev_date = prev.attribute_value
      if prev_date
        Date.new( prev_date.year, prev_date.month, $1.to_i )
      else
        value
      end

    # Mostly to fix date strings that have come
    # out of the db and been formatted
    # Accepts dd mmm yy.
    # Assumes 20 as the century.
    when [:date,:datetime].include?( field.meta.type ) && value =~ %r{^(\d{2})[ /-](\w{3})[ /-](\d{2})$}
      Date.parse( "#$1 #$2 20#$3" )

    # allow lots of flexibility in entering times
    # 01:17, 0117, 117, 1 17, are all accepted
    when field.meta.type == :time && value =~ %r{^(\d{1,2}).?(\d{2})$}
      Time.parse( "#$1:#$2" )

    # remove thousand separators, allow for space and comma
    # instead of . as a decimal separator
    when field.meta.type == :decimal
      # do various transforms
      case
        # accept a space or a comma instead of a . for floats
        # as long as there are only 2 decimal places
        when value =~ /(.*?)(\d)[ ,](\d{2})$/
          "#$1#$2.#$3"
        else
          value
      end.gsub( ',', '' ) # strip remaining commas

    else
      value
  end
end


83
84
85
86
87
88
89
# File 'lib/clevic/field_valuer.rb', line 83

def find_related( attribute, value )
  candidates = field.related_class.filter( attribute.to_sym => value ).all
  if candidates.size != 1
    raise "#{candidates.size} != 1 candidates for #{value}: #{candidates.inspect}"
  end
  candidates.first
end

#raw_valueObject

the value for this index used to be gui_value, but that wasn’t right



9
10
11
# File 'lib/clevic/field_valuer.rb', line 9

def raw_value
  field.value_for( entity )
end

#text_value=(value) ⇒ Object

set the field value from a value that could be a text representation a-la edit_value, or possible a name from a related entity



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clevic/field_valuer.rb', line 94

def text_value=( value )
  case field.display
  when Symbol
    # we have a related class of some kind, 
    self.attribute_value = find_related( field.display, value )

  when NilClass
    self.edit_value = value

  when String
    # allow plain strings, but not dotted paths
    if field.display['.']
      raise "display (#{field.display}) is a dotted path"
    else
      self.attribute_value = find_related( field.display.to_sym, value )
    end

  else
    raise "display is a not a symbol or nil: #{display.inspect}"
  end
end

#tooltipObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/clevic/field_valuer.rb', line 136

def tooltip
  case
    # show validation errors
    when has_errors?
      errors.join("\n")

    # provide a tooltip when an empty relational field is encountered
    # TODO should be part of field definition
    when field.meta.type == :association
      field.delegate.if_empty_message

    # read-only field
    when field.read_only?
      field.tooltip_for( entity ) || 'Read-only'

    else
      field.tooltip_for( entity )
  end
end

#valuer(entity) ⇒ Object



164
165
166
# File 'lib/clevic/field_valuer.rb', line 164

def valuer( entity )
  Valuer.new(field,entity)
end

#writerObject

cache the writer method name since it’s not likely to change



125
126
127
# File 'lib/clevic/field_valuer.rb', line 125

def writer
  @writer ||= "#{field.attribute.to_s}="
end