Class: RubyWMI::Base
- Inherits:
-
Object
- Object
- RubyWMI::Base
- Defined in:
- lib/ruby-wmi/base.rb
Overview
Many of the methods in Base are borrowed directly, or with some modification from ActiveRecord
http://api.rubyonrails.org/classes/ActiveRecord/Base.html
Class Method Summary collapse
-
.all(options = {}) ⇒ Object
an alias for find(:all).
- .clear_connection_options ⇒ Object
- .columns ⇒ Object
- .connection_options ⇒ Object
- .count(options = {}) ⇒ Object
-
.find(arg = :all, options = {}) ⇒ Object
WMI::Win32_ComputerSystem.find(:all) returns an array of Win32ComputerSystem objects.
-
.find_by_wql(query) ⇒ Object
#find_by_wql currently only works when called through #find it may stay like that too.
-
.first(options = {}) ⇒ Object
an alias for find(:first).
- .host(hostname) ⇒ Object
-
.last(options = {}) ⇒ Object
an alias for find(:last).
- .logger ⇒ Object
- .logger=(logger) ⇒ Object
- .method_missing(method_id, *arguments) ⇒ Object
- .namespace ⇒ Object
- .set_connection(options = {}) ⇒ Object
- .set_wmi_class_name(name) ⇒ Object
- .set_wmi_namespace(namespace) ⇒ Object
- .subclass_name ⇒ Object
- .writable?(key) ⇒ Boolean
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #camelize(string) ⇒ Object
-
#initialize(win32ole_object) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #methods(all_methods = true) ⇒ Object
- #name ⇒ Object
- #underscore(string) ⇒ Object
Constructor Details
#initialize(win32ole_object) ⇒ Base
Returns a new instance of Base.
346 347 348 |
# File 'lib/ruby-wmi/base.rb', line 346 def initialize(win32ole_object) @win32ole_object = win32ole_object end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
389 390 391 392 |
# File 'lib/ruby-wmi/base.rb', line 389 def method_missing(name,*args) name = camelize(name.to_s) @win32ole_object.send(name) end |
Class Method Details
.all(options = {}) ⇒ Object
an alias for find(:all)
98 99 100 |
# File 'lib/ruby-wmi/base.rb', line 98 def all(={}) find(:all, ) end |
.clear_connection_options ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/ruby-wmi/base.rb', line 130 def @host = nil @user = nil @password = nil @namespace = nil @privileges = nil .clear end |
.columns ⇒ Object
164 165 166 |
# File 'lib/ruby-wmi/base.rb', line 164 def columns @columns ||= first(:from => 'meta_class', :conditions => "__this ISA '#{subclass_name}'").attribute_names end |
.connection_options ⇒ Object
160 161 162 |
# File 'lib/ruby-wmi/base.rb', line 160 def @connection_options ||= {} end |
.count(options = {}) ⇒ Object
102 103 104 |
# File 'lib/ruby-wmi/base.rb', line 102 def count(={}) find(:all, ).size end |
.find(arg = :all, options = {}) ⇒ Object
WMI::Win32_ComputerSystem.find(:all)
returns an array of Win32ComputerSystem objects
WMI::Win32_ComputerSystem.find(:first)
returns the first Win32_ComputerSystem object
WMI::Win32_ComputerSystem.find(:last)
returns the last Win32_ComputerSystem object
options:
:conditions
Conditions can either be specified as a string, array, or hash representing the WHERE-part of an SQL statement.
The array form is to be used when the condition input is tainted and requires sanitization. The string form can
be used for statements that don't involve tainted data. The hash form works much like the array form.
Hash examples:
WMI::Win32_ComputerSystem.find(:all, :conditions => {:drive_type => 3} )
WMI::Win32_NTLogEvent.find(:all, :conditions => {:time_written => time_range})
Array examples:
WMI::Win32_ComputerSystem.find(:all, :conditions => ['DriveType = ?', 3] )
WMI::Win32_Service.all(:conditions => ["StartMode = ? AND Started = ?", 'manual', true])
WMI::Win32_Process.all(:conditions => ["Name LIKE %s", 'G%'])
String example:
WMI::Win32_ComputerSystem.find(:all, :conditions => 'DriveType = 3' )
:select
By default, this is "*" as in "SELECT * FROM", but can be changed.
Takes a string or symbol with a single column (e.g. "id"), or an array with
multiple columns, (e.g. [:start_mode, :start_name, :status] )
Column names are converted from underscore to camelcase, so :start_mode
becomes StartMode
:host - computername, defaults to localhost
:namespace - swebm namespace , defaults to 'root\\cimv2'
:privileges - see WMI::Privilege for a list of privileges
:user - username (domain\\username)
:password - password
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby-wmi/base.rb', line 72 def find(arg=:all, ={}) set_connection case arg when :all find_all() when :first find_first() when :last find_last() when String .merge!(:conditions => { :name => arg }) find_first() end end |
.find_by_wql(query) ⇒ Object
#find_by_wql currently only works when called through #find it may stay like that too. I haven’t decided.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby-wmi/base.rb', line 8 def find_by_wql(query) # TODO: add logging, ie: logger.debug query if logger d = connection.ExecQuery(query) begin d.count # needed to check for errors. Weird, but it works. rescue => error case error.to_s when /Invalid class/i raise InvalidClass when /Invalid query/i raise InvalidQuery when /Invalid namespace/i raise InvalidNameSpace end end d.map{|wmi_object| new(wmi_object) } end |
.first(options = {}) ⇒ Object
an alias for find(:first)
93 94 95 |
# File 'lib/ruby-wmi/base.rb', line 93 def first(={}) find(:first, ) end |
.host(hostname) ⇒ Object
155 156 157 158 |
# File 'lib/ruby-wmi/base.rb', line 155 def host(hostname) [:host] = hostname.to_s self end |
.last(options = {}) ⇒ Object
an alias for find(:last)
88 89 90 |
# File 'lib/ruby-wmi/base.rb', line 88 def last(={}) find(:last, ) end |
.logger ⇒ Object
178 179 180 |
# File 'lib/ruby-wmi/base.rb', line 178 def logger @@logger ||= nil end |
.logger=(logger) ⇒ Object
182 183 184 |
# File 'lib/ruby-wmi/base.rb', line 182 def logger=(logger) @@logger = logger end |
.method_missing(method_id, *arguments) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ruby-wmi/base.rb', line 106 def method_missing(method_id, *arguments) if match = /find_(all_by|by)_([_a-zA-Z]\w*)/.match(method_id.to_s) case match[1] when 'all_by' conditions = match[2].split('_and_').zip(arguments) conditions = Hash[*conditions.flatten] all(:conditions => conditions) when 'by' first(:conditions => {match[2] => arguments.first}) end else super end end |
.namespace ⇒ Object
147 148 149 |
# File 'lib/ruby-wmi/base.rb', line 147 def namespace @namespace_ ||= 'root\\cimv2' end |
.set_connection(options = {}) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/ruby-wmi/base.rb', line 122 def set_connection(={}) @host = [:host].to_s || [:host].to_s @user = [:user] || [:user] @password = [:password] || [:password] @namespace = [:namespace] || self.namespace @privileges = [:privileges] end |
.set_wmi_class_name(name) ⇒ Object
139 140 141 |
# File 'lib/ruby-wmi/base.rb', line 139 def set_wmi_class_name(name) @subclass_name = name end |
.set_wmi_namespace(namespace) ⇒ Object
143 144 145 |
# File 'lib/ruby-wmi/base.rb', line 143 def set_wmi_namespace(namespace) @namespace_ = namespace end |
.subclass_name ⇒ Object
151 152 153 |
# File 'lib/ruby-wmi/base.rb', line 151 def subclass_name @subclass_name ||= self.name.split('::').last end |
.writable?(key) ⇒ Boolean
168 169 170 171 172 173 174 175 176 |
# File 'lib/ruby-wmi/base.rb', line 168 def writable?(key) obj_def = connection.get(subclass_name) key = camelize(key) key_prop = obj_def.properties_(key) key_prop.qualifiers_.each do |q| return true if q.name == 'write' end false end |
Instance Method Details
#[](key) ⇒ Object
377 378 379 380 |
# File 'lib/ruby-wmi/base.rb', line 377 def [](key) key = camelize(key.to_s) @win32ole_object[key] end |
#[]=(key, value) ⇒ Object
382 383 384 385 386 387 |
# File 'lib/ruby-wmi/base.rb', line 382 def []=(key,value) key = camelize(key.to_s) raise ReadOnlyError unless writable?(key) @win32ole_object[key] = value @win32ole_object.Put_ end |
#attribute_names ⇒ Object
373 374 375 |
# File 'lib/ruby-wmi/base.rb', line 373 def attribute_names @attribute_names ||= @win32ole_object.properties_.map{ |p| underscore(p.name) } end |
#attributes ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/ruby-wmi/base.rb', line 354 def attributes if @attributes return @attributes else @attributes = {} @win32ole_object.properties_.each{ |prop| name = prop.name value = @win32ole_object.send(name) value = if prop.cimtype == 101 && value Time.parse_swbem_date_time(value) else value end @attributes[underscore(name)] = value } return @attributes end end |
#camelize(string) ⇒ Object
394 395 396 |
# File 'lib/ruby-wmi/base.rb', line 394 def camelize(string) self.class.send(:camelize, string) end |
#inspect ⇒ Object
402 403 404 |
# File 'lib/ruby-wmi/base.rb', line 402 def inspect "#<#{self.class}:#{name}>" end |
#methods(all_methods = true) ⇒ Object
350 351 352 |
# File 'lib/ruby-wmi/base.rb', line 350 def methods(all_methods=true) super(all_methods) + @win32ole_object.methods_.map{|method| underscore(method.name) } end |
#name ⇒ Object
406 407 408 409 410 411 412 413 414 |
# File 'lib/ruby-wmi/base.rb', line 406 def name @win32ole_object.name rescue begin @win32ole_object.description rescue object_id end end |
#underscore(string) ⇒ Object
398 399 400 |
# File 'lib/ruby-wmi/base.rb', line 398 def underscore(string) self.class.send(:underscore, string) end |