Class: Sane::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/sane/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Device

Returns a new instance of Device.



5
6
7
8
9
10
11
# File 'lib/sane/device.rb', line 5

def initialize(options)
  @name = options[:name]
  @vendor = options[:vendor]
  @model = options[:model]
  @type = options[:type]
  @handle = nil
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/sane/device.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/sane/device.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/sane/device.rb', line 3

def type
  @type
end

#vendorObject (readonly)

Returns the value of attribute vendor.



3
4
5
# File 'lib/sane/device.rb', line 3

def vendor
  @vendor
end

Instance Method Details

#[](option) ⇒ Object



74
75
76
77
# File 'lib/sane/device.rb', line 74

def [](option)
  ensure_open!
  Sane.instance.send(:get_option, @handle, option_lookup(option))
end

#[]=(option, value) ⇒ Object



79
80
81
82
# File 'lib/sane/device.rb', line 79

def []=(option, value)
  ensure_open!
  Sane.instance.send(:set_option, @handle, option_lookup(option), value)
end

#asyncObject



59
60
61
62
# File 'lib/sane/device.rb', line 59

def async
  ensure_open!
  Sane.instance.send(:set_io_mode, @handle, true)
end

#cancelObject



49
50
51
52
# File 'lib/sane/device.rb', line 49

def cancel
  ensure_open!
  Sane.instance.send(:cancel, @handle)
end

#closeObject



33
34
35
36
37
# File 'lib/sane/device.rb', line 33

def close
  ensure_open!
  Sane.instance.send(:close, @handle)
  @handle = nil
end

#closed?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sane/device.rb', line 13

def closed?
  @handle.nil?
end

#describe(option) ⇒ Object



115
116
117
# File 'lib/sane/device.rb', line 115

def describe(option)
  option_descriptors[option_lookup(option)]
end

#inspectObject



119
120
121
# File 'lib/sane/device.rb', line 119

def inspect
  %Q{#<#{self.class.name}:"#{name}">}
end

#openObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sane/device.rb', line 21

def open
  ensure_closed!
  @handle = Sane.instance.send(:open, @name)
  if block_given?
    begin
      yield(self)
    ensure
      close
    end
  end
end

#open?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/sane/device.rb', line 17

def open?
  !closed?
end

#option_countObject



64
65
66
67
# File 'lib/sane/device.rb', line 64

def option_count
  ensure_open!
  Sane.instance.send(:get_option, @handle, 0)
end

#option_descriptorsObject



84
85
86
# File 'lib/sane/device.rb', line 84

def option_descriptors
  option_count.times.map { |i| Sane.instance.send(:get_option_descriptor, @handle, i) }
end

#option_lookup(option_name) ⇒ Object



110
111
112
113
# File 'lib/sane/device.rb', line 110

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}")
end

#option_namesObject



88
89
90
# File 'lib/sane/device.rb', line 88

def option_names
  option_descriptors.map { |option| option[:name] }
end

#option_valuesObject



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sane/device.rb', line 92

def option_values
  option_count.times.map do |i|
    begin
      self[i]
    rescue Error => e
      if e.status == :inval
        nil # we can't read values of some options (i.e. buttons), ignore them
      else
        raise e
      end
    end
  end
end

#optionsObject



106
107
108
# File 'lib/sane/device.rb', line 106

def options
  Hash[*option_names.zip(option_values).flatten]
end

#parametersObject



69
70
71
72
# File 'lib/sane/device.rb', line 69

def parameters
  ensure_open!
  Sane.instance.send(:get_parameters, @handle)
end

#readObject



44
45
46
47
# File 'lib/sane/device.rb', line 44

def read
  ensure_open!
  Sane.instance.send(:read, @handle)
end

#startObject



39
40
41
42
# File 'lib/sane/device.rb', line 39

def start
  ensure_open!
  Sane.instance.start(@handle)
end

#syncObject



54
55
56
57
# File 'lib/sane/device.rb', line 54

def sync
  ensure_open!
  Sane.instance.send(:set_io_mode, @handle, false)
end