Class: Selekt::SourceStub
- Inherits:
-
Object
- Object
- Selekt::SourceStub
- Defined in:
- lib/selekt/source_stub.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_row(row) ⇒ Object (also: #<<, #push)
- #add_rows(rows) ⇒ Object (also: #concat)
-
#initialize(*fields) ⇒ SourceStub
constructor
A new instance of SourceStub.
- #size ⇒ Object (also: #length)
- #sql ⇒ Object
Constructor Details
#initialize(*fields) ⇒ SourceStub
Returns a new instance of SourceStub.
5 6 7 8 |
# File 'lib/selekt/source_stub.rb', line 5 def initialize(*fields) @fields = fields.map { |f| f.to_sym } @rows = [] end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
3 4 5 |
# File 'lib/selekt/source_stub.rb', line 3 def fields @fields end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
3 4 5 |
# File 'lib/selekt/source_stub.rb', line 3 def rows @rows end |
Instance Method Details
#==(other) ⇒ Object
40 41 42 43 |
# File 'lib/selekt/source_stub.rb', line 40 def ==(other) return false unless other.is_a?(Selekt::SourceStub) fields == other.fields && rows == other.rows end |
#add_row(row) ⇒ Object Also known as: <<, push
10 11 12 13 14 15 16 17 18 |
# File 'lib/selekt/source_stub.rb', line 10 def add_row(row) if row.is_a?(Hash) @rows << fields.map { |f| row[f] } else raise Selekt::StubError, "Row should have #{fields.size} values maximum" if fields.size < row.size @rows << fields.map.with_index { |_, i| row[i] } end return self end |
#add_rows(rows) ⇒ Object Also known as: concat
23 24 25 26 |
# File 'lib/selekt/source_stub.rb', line 23 def add_rows(rows) rows.each { |row| add_row(row) } return self end |
#size ⇒ Object Also known as: length
36 37 38 |
# File 'lib/selekt/source_stub.rb', line 36 def size @rows.size end |
#sql ⇒ Object
30 31 32 33 34 |
# File 'lib/selekt/source_stub.rb', line 30 def sql first_row_sql = [row_sql_with_names(rows[0])] other_row_sql = rows[1..-1].map { |row| row_sql_without_names(row) } [first_row_sql].concat(other_row_sql).join("\nUNION ALL\n") end |