Class: Chair::Row

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/chair/row.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, id, data = {}) ⇒ Row

Create a new cell

Parameters:

  • table (Table)

    the table holding this row

  • id (Fixnum)

    the array index of this row in the internal 2D array



7
8
9
10
11
12
13
14
# File 'lib/chair/row.rb', line 7

def initialize(table, id, data = {})
  @row_id = id
  @table = table
  @attributes = data
  @attributes.clone.
      keep_if{ |col, _| @table.indices.include? col }.
      each_pair{ |col, val| add_to_index(col, val) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/chair/row.rb', line 91

def method_missing(method_sym, *arguments, &block)
  # the first argument is a Symbol, so you need to_s it if you want to pattern match
  if method_sym.to_s =~ /^has_(.*)\?$/
    has_attribute?($1.to_sym)
  else
    super
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compare rows within a table

Parameters:

  • other (Object)

    the object to compare to



64
65
66
67
68
69
70
71
72
# File 'lib/chair/row.rb', line 64

def <=>(other)
  # only be comparable if we're in the same table
  if @table.equal?(other.instance_variable_get(@table))
    other_id = other.instance_variable_get('@id')
    @id <=> other_id
  else
    nil
  end
end

#==(other) ⇒ Bool

Compare Row instances based on internal representation

Parameters:

  • other (Object)

    the object to compare to

Returns:

  • (Bool)

    whether or not the objects are the same



52
53
54
55
56
57
58
59
60
# File 'lib/chair/row.rb', line 52

def ==(other)
  case other
    when Chair::Row
      @attributes == other.instance_variable_get('@attributes')
    when Array
      @attributes.values == other
    else false
  end
end

#[](column) ⇒ Object?

Get a cell based on the column name

Returns:

  • (Object, nil)

    the value in the cell, can be nil



18
19
20
# File 'lib/chair/row.rb', line 18

def [](column)
  @attributes[column]
end

#[]=(column, value) ⇒ Object

Assign a new value to one of the cells in the row

Parameters:

  • column (Symbol)

    the column name to add to

  • value (Object)

    the value to assign

Returns:

  • (Object)

    the assigned value



26
27
28
29
30
31
# File 'lib/chair/row.rb', line 26

def []=(column, value)
  if @table.indices.include? column
    add_to_index(column, value)
  end
  @attributes[column] = value
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/chair/row.rb', line 33

def empty?
  @attributes.empty?
end

#has_attribute?(name) ⇒ Bool

Looks to see if we have a attribute

Parameters:

  • name (Symbol)

    the column to look at

Returns:

  • (Bool)

    whether or not the value is empty



86
87
88
89
# File 'lib/chair/row.rb', line 86

def has_attribute?(name)
  val = @attributes[name]
  val.nil? ? false : (not val.empty?)
end

#inspectObject

Returns the contents of the record as a nicely formatted string.



75
76
77
78
79
80
81
# File 'lib/chair/row.rb', line 75

def inspect
  pairs = []
  # Use the table's column list to order our columns
  @table.columns.each { |name| pairs << "#{name}: #{@attributes[name].inspect}"}
  inspection = pairs.compact.join(', ')
  "#<#{self.class} #{inspection}>"
end

#to_aArray<Object>

Convert the row data to an array

Returns:

  • (Array<Object>)

    the data in the row



45
46
47
# File 'lib/chair/row.rb', line 45

def to_a
  @table.columns.map { |col| @attributes[col] }
end

#to_hashHash<Symbol, Object>

Create a hash of the data based on the columns

Returns:

  • (Hash<Symbol, Object>)

    the data in the row



39
40
41
# File 'lib/chair/row.rb', line 39

def to_hash
  @attributes
end