Class: Moped::BSON::Code

Inherits:
Object show all
Defined in:
lib/moped/bson/code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, scope = nil) ⇒ Code

Returns a new instance of Code.



7
8
9
10
# File 'lib/moped/bson/code.rb', line 7

def initialize(code, scope=nil)
  @code = code
  @scope = scope
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/moped/bson/code.rb', line 5

def code
  @code
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/moped/bson/code.rb', line 5

def scope
  @scope
end

Class Method Details

.__bson_load__(io) ⇒ Object



26
27
28
29
# File 'lib/moped/bson/code.rb', line 26

def __bson_load__(io)
  code = io.read(*io.read(4).unpack(INT32_PACK)).from_utf8_binary.chop!
  new code
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



16
17
18
# File 'lib/moped/bson/code.rb', line 16

def ==(other)
  BSON::Code === other && code == other.code && scope == other.scope
end

#__bson_dump__(io, key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moped/bson/code.rb', line 32

def __bson_dump__(io, key)
  if scoped?
    io << Types::CODE_WITH_SCOPE
    io << key.to_bson_cstring

    code_start = io.bytesize

    io << START_LENGTH

    data = code.to_utf8_binary
    io << [data.bytesize+1].pack(INT32_PACK)
    io << data
    io << NULL_BYTE

    scope.__bson_dump__(io)

    io[code_start, 4] = [io.bytesize - code_start].pack(INT32_PACK)

  else
    io << Types::CODE
    io << key.to_bson_cstring

    data = code.to_utf8_binary
    io << [data.bytesize+1].pack(INT32_PACK)
    io << data
    io << NULL_BYTE
  end
end

#hashObject



21
22
23
# File 'lib/moped/bson/code.rb', line 21

def hash
  [code, scope].hash
end

#scoped?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/moped/bson/code.rb', line 12

def scoped?
  !!scope
end