Class: SqlPostgres::PgBit

Inherits:
PgType
  • Object
show all
Defined in:
lib/sqlpostgres/PgBit.rb

Overview

This class holds the value of a “bit” column.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PgType

#eql?, #hash, #to_sql

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

#bitsObject (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_sObject

Return a string representation (ie “01011”).



45
46
47
# File 'lib/sqlpostgres/PgBit.rb', line 45

def to_s
  bits.join
end