Class: Chair::Row
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare rows within a table.
-
#==(other) ⇒ Bool
Compare Row instances based on internal representation.
-
#[](column) ⇒ Object?
Get a cell based on the column name.
-
#[]=(column, value) ⇒ Object
Assign a new value to one of the cells in the row.
- #empty? ⇒ Boolean
-
#has_attribute?(name) ⇒ Bool
Looks to see if we have a attribute.
-
#initialize(table, id, data = {}) ⇒ Row
constructor
Create a new cell.
-
#inspect ⇒ Object
Returns the contents of the record as a nicely formatted string.
- #method_missing(method_sym, *arguments, &block) ⇒ Object
-
#to_a ⇒ Array<Object>
Convert the row data to an array.
-
#to_hash ⇒ Hash<Symbol, Object>
Create a hash of the data based on the columns.
Constructor Details
#initialize(table, id, data = {}) ⇒ Row
Create a new cell
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
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
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
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
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
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
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 |
#inspect ⇒ Object
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_a ⇒ Array<Object>
Convert the row data to an array
45 46 47 |
# File 'lib/chair/row.rb', line 45 def to_a @table.columns.map { |col| @attributes[col] } end |
#to_hash ⇒ Hash<Symbol, Object>
Create a hash of the data based on the columns
39 40 41 |
# File 'lib/chair/row.rb', line 39 def to_hash @attributes end |