Class: Arel::Middleware::Result
- Inherits:
-
Object
- Object
- Arel::Middleware::Result
- Defined in:
- lib/arel/middleware/result.rb
Overview
Class is very similar to ActiveRecord::Result activerecord/lib/active_record/result.rb
Instance Attribute Summary collapse
-
#original_data ⇒ Object
readonly
Returns the value of attribute original_data.
Class Method Summary collapse
Instance Method Summary collapse
- #column_objects ⇒ Object
- #columns ⇒ Object
- #hash_rows ⇒ Object
-
#initialize(from_caster, to_caster, original_data) ⇒ Result
constructor
A new instance of Result.
- #modified? ⇒ Boolean
- #remove_column(column_name) ⇒ Object
- #rows ⇒ Object
- #to_casted_result ⇒ Object
Constructor Details
#initialize(from_caster, to_caster, original_data) ⇒ Result
Returns a new instance of Result.
22 23 24 25 26 27 |
# File 'lib/arel/middleware/result.rb', line 22 def initialize(from_caster, to_caster, original_data) @from_caster = from_caster @to_caster = to_caster @original_data = original_data @modified = false end |
Instance Attribute Details
#original_data ⇒ Object (readonly)
Returns the value of attribute original_data.
16 17 18 |
# File 'lib/arel/middleware/result.rb', line 16 def original_data @original_data end |
Class Method Details
Instance Method Details
#column_objects ⇒ Object
33 34 35 |
# File 'lib/arel/middleware/result.rb', line 33 def column_objects @column_objects ||= from_caster.column_objects(original_data) end |
#columns ⇒ Object
29 30 31 |
# File 'lib/arel/middleware/result.rb', line 29 def columns @columns ||= column_objects.map(&:name) end |
#hash_rows ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/arel/middleware/result.rb', line 60 def hash_rows @hash_rows ||= begin rows.map do |row| hash = {} index = 0 length = columns.length while index < length hash[columns[index]] = row[index] index += 1 end hash end end end |
#modified? ⇒ Boolean
83 84 85 |
# File 'lib/arel/middleware/result.rb', line 83 def modified? @modified end |
#remove_column(column_name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/arel/middleware/result.rb', line 41 def remove_column(column_name) column_index = columns.index(column_name) raise "Unknown column `#{column_name}`. Existing columns: `#{columns}`" if column_index.nil? @hash_rows = nil @columns = nil @modified = true column_objects.delete_at(column_index) deleted_rows = [] rows.map! do |row| deleted_rows << row.delete_at(column_index) row end deleted_rows end |
#rows ⇒ Object
37 38 39 |
# File 'lib/arel/middleware/result.rb', line 37 def rows @rows ||= from_caster.rows(original_data) end |
#to_casted_result ⇒ Object
79 80 81 |
# File 'lib/arel/middleware/result.rb', line 79 def to_casted_result to_caster.cast_to(self) end |