class SqlPostgres::PgBit
This class holds the value of a “bit” column.
Attributes
bits[R]
Return an array of 0's and 1's with the bits.
Public Class Methods
from_sql(s)
click to toggle source
Create a PgBit
from a string in Postgres format (ie “(1,2)”).
# 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
new(*args)
click to toggle source
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")
# 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
Public Instance Methods
to_s()
click to toggle source
Return a string representation (ie “01011”).
# File lib/sqlpostgres/PgBit.rb, line 45 def to_s bits.join end
Protected Instance Methods
parts()
click to toggle source
# File lib/sqlpostgres/PgBit.rb, line 51 def parts [bits] end
Private Instance Methods
bits_from_sql(s)
click to toggle source
# File lib/sqlpostgres/PgBit.rb, line 61 def bits_from_sql(s) s.scan(/./).collect do |d| d.to_i end end
column_type()
click to toggle source
# File lib/sqlpostgres/PgBit.rb, line 57 def column_type 'bit' end