Class: SqlPostgres::PgBit
Overview
This class holds the value of a “bit” column.
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Return an array of 0’s and 1’s with the bits.
Class Method Summary collapse
-
.from_sql(s) ⇒ Object
Create a PgBit from a string in Postgres format (ie “(1,2)”).
Instance Method Summary collapse
-
#initialize(*args) ⇒ PgBit
constructor
Constructor.
-
#to_s ⇒ Object
Return a string representation (ie “01011”).
Methods inherited from PgType
Constructor Details
#initialize(*args) ⇒ PgBit
Constructor. Takes either an array of bits, a bunch of bits, or a string. These are all equivalent:
PgBit.new([0, 1, 0, 1])
PgBit.new(0, 1, 0, 1)
PgBit.new("0101")
34 35 36 37 38 39 40 41 |
# File 'lib/sqlpostgres/PgBit.rb', line 34 def initialize(*args) args = args.flatten if args.size == 1 && args[0].is_a?(String) @bits = bits_from_sql(args[0]) else @bits = args end end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Return an array of 0’s and 1’s with the bits.
11 12 13 |
# File 'lib/sqlpostgres/PgBit.rb', line 11 def bits @bits end |
Class Method Details
.from_sql(s) ⇒ Object
Create a PgBit from a string in Postgres format (ie “(1,2)”).
18 19 20 21 22 23 24 |
# File 'lib/sqlpostgres/PgBit.rb', line 18 def from_sql(s) if s =~ /^[01]*$/ PgBit.new(s) else raise ArgumentError, "Invalid bit: #{s.inspect}" end end |
Instance Method Details
#to_s ⇒ Object
Return a string representation (ie “01011”).
45 46 47 |
# File 'lib/sqlpostgres/PgBit.rb', line 45 def to_s bits.join end |