Class: Pwnbus::Configdb::Db::Proxy

Inherits:
BasicObject
Defined in:
lib/pwnbus/configdb/db.rb,
lib/pwnbus/configdb/db.rb

Overview

Easy access to a configuration database.

Instance Method Summary collapse

Constructor Details

#initialize(database, prefix) ⇒ Proxy

Returns a new instance of Proxy.



108
109
110
111
112
# File 'lib/pwnbus/configdb/db.rb', line 108

def initialize(database, prefix)
  @db = database
  @prefix = prefix
  @eigenclass = (class <<self; self; end)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Pretty access



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pwnbus/configdb/db.rb', line 125

def method_missing(name, *args)
  db_key = @prefix + name.to_s
  do_write = false
  
  case db_key[-1]
  when ??
    db_key = db_key[0...-1]
  when ?=
    db_key = db_key[0...-1]
    do_write = true
  end
  
  if do_write
    @eigenclass.class_eval <<END_DEF
      def #{name}(value)
        @db.proxy_set #{db_key.inspect}, value
      end
END_DEF
  else
    @eigenclass.class_eval <<END_DEF
      def #{name}
        @db.proxy_get #{db_key.inspect}
      end
END_DEF
  end
  
  # The method was defined, now fire it off.
  send name, *args
end

Instance Method Details

#empty?Boolean

Proxy objects aren’t database values, so they behave like nil.

Returns:

  • (Boolean)


120
121
122
# File 'lib/pwnbus/configdb/db.rb', line 120

def empty?
  true
end

#nil?Boolean

Proxy objects aren’t database values, so they behave like nil.

Returns:

  • (Boolean)


115
116
117
# File 'lib/pwnbus/configdb/db.rb', line 115

def nil?
  true
end