diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..2d53849 --- /dev/null +++ b/README.rdoc @@ -0,0 +1,7 @@ += SANE FFI + +Scanner Access Now Easier in Ruby. + +== Copyright + +Copyright (c) 2011 Jakub Kuźma diff --git a/lib/sane.rb b/lib/sane.rb index 8c43653..9bb1fe1 100644 --- a/lib/sane.rb +++ b/lib/sane.rb @@ -15,7 +15,7 @@ class Sane def self.open instance.send(:init) - yield instance + yield(instance) ensure instance.send(:exit) end @@ -52,12 +52,12 @@ class Sane devices_pointer_pointer = FFI::MemoryPointer.new(:pointer) check_status!(API.sane_get_devices(devices_pointer_pointer, 0)) devices_pointer = devices_pointer_pointer.read_pointer - [].tap do |result| - until devices_pointer.read_pointer.null? - result << API::Device.new(devices_pointer.read_pointer).to_hash - devices_pointer += FFI.type_size(:pointer) - end + result = [] + until devices_pointer.read_pointer.null? + result << API::Device.new(devices_pointer.read_pointer).to_hash + devices_pointer += FFI.type_size(:pointer) end + result end def open(device_name) @@ -156,14 +156,14 @@ class Sane end def ensure_not_initialized! - raise "SANE library is already initialized" if initialized? + raise("SANE library is already initialized") if initialized? end def ensure_initialized! - raise "SANE library is not initialized" if not_initialized? + raise("SANE library is not initialized") if not_initialized? end def check_status!(status) - raise Error.new(strstatus(status), status) unless status == :good + raise(Error.new(strstatus(status), status)) unless status == :good end end diff --git a/lib/sane/api.rb b/lib/sane/api.rb index 99d001a..b8a78d4 100644 --- a/lib/sane/api.rb +++ b/lib/sane/api.rb @@ -13,7 +13,14 @@ class Sane class Device < FFI::Struct layout :name, :string, :vendor, :string, :model, :string, :type, :string - def to_hash; {name: self[:name], vendor: self[:vendor], model: self[:model], type: self[:type]} end + def to_hash + { + :name => self[:name], + :vendor => self[:vendor], + :model => self[:model], + :type => self[:type] + } + end end class OptionDescriptor < FFI::Struct @@ -22,13 +29,32 @@ class Sane end layout :name, :string, :title, :string, :desc, :string, :type, :value_type, :unit, :unit, :size, :int, :cap, :int, :constraint_type, ConstraintType - def to_hash; {name: self[:name], title: self[:title], desc: self[:desc], type: self[:type], unit: self[:unit], size: self[:size], cap: self[:cap]} end + def to_hash; + { + :name => self[:name], + :title => self[:title], + :desc => self[:desc], + :type => self[:type], + :unit => self[:unit], + :size => self[:size], + :cap => self[:cap] + } + end end class Parameters < FFI::Struct layout :format, :frame, :last_frame, :int, :bytes_per_line, :int, :pixels_per_line, :int, :lines, :int, :depth, :int - def to_hash; {format: self[:format], last_frame: self[:last_frame], bytes_per_line: self[:bytes_per_line], pixels_per_line: self[:pixels_per_line], lines: self[:lines], depth: self[:depth]} end + def to_hash + { + :format => self[:format], + :last_frame => self[:last_frame], + :bytes_per_line => self[:bytes_per_line], + :pixels_per_line => self[:pixels_per_line], + :lines => self[:lines], + :depth => self[:depth] + } + end end # extern SANE_Status sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize); diff --git a/lib/sane/device.rb b/lib/sane/device.rb index 757f5fa..8e24acd 100644 --- a/lib/sane/device.rb +++ b/lib/sane/device.rb @@ -77,18 +77,20 @@ class Sane begin self[i] rescue Error - nil # we can't get values of some options, ignore them + nil # we can't read values of some options (i.e. buttons), ignore them end end end def options - {}.tap { |hash| option_count.times { |i| hash[option_names[i]] = option_values[i] } } + result = {} + option_count.times { |i| hash[option_names[i]] = option_values[i] } + result end def option_lookup(option_name) return option_name if (0..option_count).include?(option_name) - option_descriptors.index { |option| option[:name] == option_name.to_s } or raise ArgumentError, "Option not found: #{option_name}" + option_descriptors.index { |option| option[:name] == option_name.to_s } or raise(ArgumentError, "Option not found: #{option_name}") end def describe(option) @@ -98,11 +100,11 @@ class Sane private def ensure_closed! - raise "Device is already open" if open? + raise("Device is already open") if open? end def ensure_open! - raise "Device is closed" if closed? + raise("Device is closed") if closed? end end end diff --git a/sane-ffi.gemspec b/sane-ffi.gemspec index ec8e638..680d613 100644 --- a/sane-ffi.gemspec +++ b/sane-ffi.gemspec @@ -12,8 +12,6 @@ Gem::Specification.new do |s| s.summary = %q{SANE bindings} s.description = %q{Scanner Access now Easier} - # s.rubyforge_project = "sane" - s.add_dependency "ffi" s.files = `git ls-files`.split("\n")