Module: Clevic::TableIndex

Includes:
FieldValuer
Included in:
SwingTableIndex, Qt::ModelIndex
Defined in:
lib/clevic/table_index.rb

Overview

to be included in something that responds to model, row, column

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldValuer

#attribute_value, #attribute_value=, #display_value, #edit_value, #edit_value=, #find_related, #raw_value, #text_value=, #tooltip, valuer, #valuer, #writer

Instance Attribute Details

#entityObject

the underlying entity TODO caching doesn’t help with Qt because ModelIndex objects are extremely short-lived. Not sure about swing.



56
57
58
59
# File 'lib/clevic/table_index.rb', line 56

def entity
  return nil if model.nil?
  model.collection[row]
end

Instance Method Details

#<=>(other) ⇒ Object

sort by row, then column



81
82
83
84
85
86
87
88
# File 'lib/clevic/table_index.rb', line 81

def <=>( other )
  row_comp = self.row <=> other.row
  if row_comp == 0
    self.column <=> other.column
  else
    row_comp
  end
end

#attributeObject

return the attribute of the underlying entity corresponding to the column of this index



31
32
33
# File 'lib/clevic/table_index.rb', line 31

def attribute
  model.attributes[column]
end

#dumpObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/clevic/table_index.rb', line 13

def dump
  if valid?
  <<-EOF
  field: #{field_name} => #{field_value}
  attribute: #{attribute.inspect} => #{attribute_value.inspect}
  meta: #{meta.inspect}
  EOF
  else
    'invalid'
  end
end

#errorsObject

return a collection of errors. Unlike AR, this will always return an array that will have zero, one or many elements.



76
77
78
# File 'lib/clevic/table_index.rb', line 76

def errors
  [ entity.errors[field_name.to_sym] ].flatten
end

#fieldObject

return the Clevic::Field for this index



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

def field
  @field ||= model.field_for_index( self )
end

#field_nameObject

return the table’s field name. For associations, this would be suffixed with _id



44
45
46
# File 'lib/clevic/table_index.rb', line 44

def field_name
  meta.name
end

#field_valueObject

return the value of the field, it may be the _id value



49
50
51
# File 'lib/clevic/table_index.rb', line 49

def field_value
  entity.send( field_name )
end

#has_errors?Boolean

return true if validation failed for this indexes field

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/clevic/table_index.rb', line 64

def has_errors?
  # virtual fields don't have metadata
  if meta.nil?
    false
  else
    entity.errors.invalid?( field_name.to_sym )
  end
end

#inspectObject



90
91
92
# File 'lib/clevic/table_index.rb', line 90

def inspect
  "#<TableIndex (#{row},#{column}) '#{raw_value rescue "no raw value: #{$!.message}"}'>"
end

#metaObject

returns the list of ModelColumn metadata



36
37
38
39
40
# File 'lib/clevic/table_index.rb', line 36

def meta
  # use the optimised version
  # TODO just use the model version instead?
  field.meta
end

#prevObject



25
26
27
# File 'lib/clevic/table_index.rb', line 25

def prev
  choppy( :row => row - 1 )
end

#rcObject

return a string (row,column) suitable for displaying to users, ie 1-based not 0-based



96
97
98
# File 'lib/clevic/table_index.rb', line 96

def rc
  "(#{row+1},#{column+1})"
end