Skip to content

Commit 6dfbf91

Browse files
Gareth AdamsGareth Adams
authored andcommitted
Improve code style using Rubocop
* Rubocop styles * Deprecate setters and getters for code style Switches to better Ruby-style getters and setters instead of blindly copying the HID Api style * Improve code style line lengths * Improve documentation
1 parent 2cb849f commit 6dfbf91

14 files changed

Lines changed: 145 additions & 59 deletions

.rubocop.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
AllCops:
2+
DisplayCopNames: true
3+
DisplayStyleGuide: true
4+
TargetRubyVersion: 2.3
5+
6+
Metrics/LineLength:
7+
Max: 90
8+
9+
# This cop is enabled when TargetRubyVersion is 2.3 or higher
10+
#
11+
# Implementing its advice will help with Ruby 3.0 compatibility, but isn't a
12+
# trivial change, and could cause problems before gems are updated to properly
13+
# handle frozen strings.
14+
#
15+
# See: https://wyeworks.com/blog/2015/12/1/immutable-strings-in-ruby-2-dot-3
16+
Style/FrozenStringLiteralComment:
17+
Enabled: false
18+
19+
Style/StringLiterals:
20+
EnforcedStyle: double_quotes
21+
StyleGuide: https://www.viget.com/articles/just-use-double-quoted-ruby-strings
22+
23+
# This is one part of the style guide that doesn't express a preference, it just
24+
# suggests you make a choice
25+
Style/DotPosition:
26+
EnforcedStyle: trailing

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source 'https://rubygems.org'
1+
source "https://rubygems.org"
22

33
# Specify your gem's dependencies in hid_api.gemspec
44
gemspec

Rakefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
require "bundler/gem_tasks"
22

3-
begin
4-
require 'rspec/core/rake_task'
5-
RSpec::Core::RakeTask.new(:spec)
6-
rescue LoadError
7-
end
3+
require "rspec/core/rake_task"
4+
RSpec::Core::RakeTask.new(:spec)
85

9-
task :default => :spec
6+
task default: :spec

hid_api.gemspec

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
2-
lib = File.expand_path('../lib', __FILE__)
2+
lib = File.expand_path("../lib", __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'hid_api/version'
4+
require "hid_api/version"
55

66
Gem::Specification.new do |spec|
77
spec.name = "hid_api"
@@ -10,11 +10,13 @@ Gem::Specification.new do |spec|
1010
spec.authors = ["Gareth Adams"]
1111
spec.email = ["g@rethada.ms"]
1212

13-
spec.summary = %q{A Ruby FFI wrapper around the System11 hidapi C library}
14-
spec.description = %q{}
13+
spec.summary = "A Ruby FFI wrapper around the System11 hidapi C library"
14+
spec.description = ""
1515
spec.homepage = "https://github.com/gareth/ruby_hid_api"
1616

17-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
18+
f.match(%r{^(test|spec|features)/})
19+
end
1820
spec.bindir = "exe"
1921
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2022
spec.require_paths = ["lib"]

lib/hid_api.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
require 'ffi'
1+
require "ffi"
22

3+
# A Ruby wrapper around the C `hidapi` library
34
module HidApi
45
class HidError < StandardError; end
56

67
extend FFI::Library
7-
ffi_lib 'hidapi'
8+
ffi_lib "hidapi"
89

9-
autoload :Device, 'hid_api/device'
10-
autoload :DeviceInfo, 'hid_api/device_info'
11-
autoload :Util, 'hid_api/util'
12-
autoload :VERSION, 'hid_api/version'
13-
autoload :WideString, 'hid_api/wide_string'
10+
autoload :Deprecated, "hid_api/deprecated"
11+
autoload :Device, "hid_api/device"
12+
autoload :DeviceInfo, "hid_api/device_info"
13+
autoload :Util, "hid_api/util"
14+
autoload :VERSION, "hid_api/version"
15+
autoload :WideString, "hid_api/wide_string"
1416

1517
typedef DeviceInfo.auto_ptr, :hid_device_info
1618
typedef WideString, :wide_string
@@ -46,13 +48,19 @@ class << self
4648
alias exit hid_exit
4749
alias enumerate hid_enumerate
4850
alias free_enumeration hid_free_enumeration
49-
def open vendor_id, product_id, serial_number=nil
51+
def open(vendor_id, product_id, serial_number = nil)
5052
device = hid_open(vendor_id, product_id, serial_number || 0)
51-
raise HidError, "Unable to open #{[vendor_id, product_id, serial_number].inspect}" if device.null?
53+
if device.null?
54+
error = format(
55+
"Unable to open %s",
56+
[vendor_id, product_id, serial_number].inspect
57+
)
58+
raise HidError, error
59+
end
5260
device
5361
end
5462

55-
def open_path path
63+
def open_path(path)
5664
device = hid_open_path(path)
5765
raise HidError, "Unable to open #{path.inspect}" if device.null?
5866
device
@@ -61,4 +69,4 @@ def open_path path
6169
end
6270

6371
# Attempts to extend some core FFI classes with platform-aware string-handling
64-
FFI::AbstractMemory.include(HidApi::Util::WCHAR)
72+
FFI::AbstractMemory.include(HidApi::Util::WCHAR)

lib/hid_api/deprecated.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module HidApi
2+
# Adds the ability to mark some methods as deprecated.
3+
#
4+
# Uses the approach listed at
5+
# http://seejohncode.com/2012/01/09/deprecating-methods-in-ruby/
6+
module Deprecated
7+
# Define a deprecated alias for a method
8+
# @param [Symbol] name - name of method to define
9+
# @param [Symbol] replacement - name of method to (alias)
10+
def deprecated_alias(name, replacement)
11+
# Create a wrapped version
12+
define_method(name) do |*args, &block|
13+
warn "##{name} deprecated (please use ##{replacement})"
14+
send replacement, *args, &block
15+
end
16+
end
17+
18+
# Deprecate a defined method
19+
# @param [Symbol] name - name of deprecated method
20+
# @param [Symbol] replacement - name of the desired replacement
21+
def deprecated(name, replacement = nil)
22+
# Replace old method
23+
old_name = :"#{name}_without_deprecation"
24+
alias_method old_name, name
25+
# And replace it with a wrapped version
26+
define_method(name) do |*args, &block|
27+
if replacement
28+
warn "##{name} deprecated (please use ##{replacement})"
29+
else
30+
warn "##{name} deprecated"
31+
end
32+
send old_name, *args, &block
33+
end
34+
end
35+
end
36+
end

lib/hid_api/device.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
1-
require 'ffi'
1+
require "ffi"
22

33
module HidApi
4+
# An FFI pointer extended to expose HID device functionality
45
class Device < FFI::Pointer
6+
extend Deprecated
57
extend FFI::DataConverter
68
native_type FFI::Type::POINTER
79

8-
def self.from_native(value, ctx)
10+
def self.from_native(value, _ctx)
911
new(value)
1012
end
1113

1214
def close
1315
HidApi.hid_close(self)
1416
end
1517

16-
def set_nonblocking(int)
18+
def nonblocking=(int)
1719
HidApi.hid_set_nonblocking self, int
1820
end
21+
deprecated_alias :set_nonblocking, :nonblocking=
1922

20-
def get_manufacturer_string
23+
def manufacturer_string
2124
get_buffered_string :manufacturer
2225
end
26+
deprecated_alias :get_manufacturer_string, :manufacturer_string
2327

24-
def get_product_string
28+
def product_string
2529
get_buffered_string :product
2630
end
31+
deprecated_alias :get_product_string, :product_string
2732

28-
def get_serial_number_string
33+
def serial_number_string
2934
get_buffered_string :serial_number
3035
end
36+
deprecated_alias :get_serial_number_string, :serial_number_string
3137

3238
def read(length)
3339
buffer = clear_buffer length
@@ -57,11 +63,11 @@ def write(data)
5763
end
5864
end
5965

60-
def get_feature_report(data)
66+
def get_feature_report(_data)
6167
raise NotImplementedError
6268
end
6369

64-
def send_feature_report(data)
70+
def send_feature_report(_data)
6571
raise NotImplementedError
6672
end
6773

@@ -72,14 +78,15 @@ def error
7278
end
7379

7480
private
75-
def clear_buffer length
81+
82+
def clear_buffer(length)
7683
b = FFI::Buffer.new(1, length)
7784
# FFI::Buffer doesn't clear the first byte if length < 8
7885
b.put_char 0, 0
7986
b
8087
end
8188

82-
def get_buffered_string field
89+
def get_buffered_string(field)
8390
buffer = clear_buffer 255
8491
HidApi.send "hid_get_#{field}_string", self, buffer, buffer.length
8592
buffer.read_wchar_string

lib/hid_api/device_info.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'ffi'
1+
require "ffi"
22

33
module HidApi
44
# Represents the hid_device_info struct returned as part of
@@ -36,7 +36,7 @@ def self.release(pointer)
3636

3737
def inspect
3838
product_string.tap do |s|
39-
s << " (%s)" % path unless path.empty?
39+
s << format(" (%s)", path) unless path.empty?
4040
end
4141
end
4242

@@ -52,4 +52,4 @@ def each
5252
end
5353
end
5454
end
55-
end
55+
end

lib/hid_api/util.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module HidApi
2-
module Util
3-
autoload :WCHAR, 'hid_api/util/wchar'
2+
module Util # :nodoc:
3+
autoload :WCHAR, "hid_api/util/wchar"
44
end
5-
end
5+
end

lib/hid_api/util/wchar.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
module HidApi
22
module Util
3+
# A utility module that provides simple (maybe naive) platform-dependent
4+
# support for wide characters
35
module WCHAR
4-
WCHAR_T_WIDTH = RUBY_PLATFORM =~ /mswin/i ? 2 : 4
6+
WCHAR_T_WIDTH = ::RUBY_PLATFORM =~ /mswin/i ? 2 : 4
57

68
# Maps a WCHAR_T_WIDTH to the corresponding Array#pack character width
79
FORMATS = {
8-
2 => 'S',
9-
4 => 'L'
10-
}
10+
2 => "S",
11+
4 => "L"
12+
}.freeze
13+
14+
# The platform-specific Array#pack format for wide characters
15+
FORMAT = FORMATS[WCHAR_T_WIDTH].freeze
1116

1217
# Maps a WCHAR_T_WIDTH to the corresponding string encoding
13-
ENCODINGS = {
14-
2 => 'utf-16le',
15-
4 => 'utf-32le'
16-
}
18+
ENCODING = {
19+
2 => "utf-16le",
20+
4 => "utf-32le"
21+
}.freeze
22+
23+
# The platform-specific String encoding that can handle wide characters
24+
ENCODING = ENCODINGS[WCHAR_T_WIDTH].freeze
1725

18-
def read_wchar_string max_chars=nil
26+
def read_wchar_string(_max_chars = nil)
1927
buffer = []
2028
offset = 0
2129
loop do
@@ -25,8 +33,8 @@ def read_wchar_string max_chars=nil
2533
buffer << char
2634
offset += 1
2735
end
28-
buffer.pack("#{FORMATS[WCHAR_T_WIDTH]}*").force_encoding(ENCODINGS[WCHAR_T_WIDTH]).encode('utf-8')
36+
buffer.pack("#{FORMAT}*").force_encoding(ENCODING).encode("utf-8")
2937
end
3038
end
3139
end
32-
end
40+
end

0 commit comments

Comments
 (0)