Created
May 12, 2014 20:02
-
-
Save adkron/530064c88294f506723a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ffi" | |
module Scada | |
module ResultSet | |
class Statuses | |
include Enumerable | |
class Status < Struct.new(:id, :value, :condition, :error) | |
def to_s | |
if to_i == 0 | |
"open" | |
else | |
"closed" | |
end | |
end | |
def to_i | |
self.value.to_i | |
end | |
end | |
attr_accessor :error_code, :ids, :values, :conditions, :errors, :size | |
private :size= | |
def initialize(ids, result_size) | |
self.size = result_size | |
self.error_code = FFI::MemoryPointer.new(:ulong) | |
self.ids = ids | |
self.values = FFI::MemoryPointer.new(:uchar, result_size) | |
self.conditions = FFI::MemoryPointer.new(:uchar, result_size) | |
self.errors = FFI::MemoryPointer.new(:ulong, result_size) | |
end | |
def each | |
return enum(__callee__) unless block_given? | |
ruby_ids.each_with_index do |id, index| | |
yield Status.new(id, ruby_values[index], ruby_conditions[index], ruby_errors[index]) | |
end | |
end | |
def to_a | |
[error_code, ids.ids, values, conditions, errors] | |
end | |
private | |
def ruby_ids | |
@ruby_ids ||= ids.ids.read_array_of_ulong(size) | |
end | |
def ruby_values | |
@ruby_values ||= values.read_array_of_char(size) | |
end | |
def ruby_conditions | |
@ruby_conditions ||= conditions.read_array_of_uchar(size) | |
end | |
def ruby_errors | |
@ruby_errors ||= errors.read_array_of_ulong(size) | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment