rubyXL-addressing 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -1
- data/README.md +3 -1
- data/lib/rubyXL/address.rb +2 -65
- data/lib/rubyXL/addressing.rb +63 -0
- data/lib/rubyXL/addressing/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0631645fffcb1b80f72b10339b3150d1e1b5e187
|
4
|
+
data.tar.gz: e66ca645218f47ace24fa1341205807f68ecf9e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37ce540e93dda95bc1f9faef6f36b1214bbb817d74c92de09fb12184ad0e0cbb139026c92c5331e3d197a25d4f26f7d5d0d3651a0a6c0119738b2721b222dcbf
|
7
|
+
data.tar.gz: a4b54aa278663be11b1ca8aa356431da95aed9db3f933262b102181d248154e3d1a0059c48845169bb55e408e4646115463151e11661a844d61dcfce5591120d
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -2,8 +2,10 @@
|
|
2
2
|
|
3
3
|
Addressing for [rubyXL](https://github.com/weshatheleopard/rubyXL)
|
4
4
|
|
5
|
+
[](https://badge.fury.io/rb/rubyXL-addressing)
|
5
6
|
[](https://travis-ci.org/m4i/rubyXL-addressing)
|
6
7
|
[](https://codeclimate.com/github/m4i/rubyXL-addressing/coverage)
|
8
|
+
[](https://codeclimate.com/github/m4i/rubyXL-addressing)
|
7
9
|
[](https://gemnasium.com/m4i/rubyXL-addressing)
|
8
10
|
|
9
11
|
## Installation
|
@@ -79,7 +81,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
79
81
|
|
80
82
|
## Contributing
|
81
83
|
|
82
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/m4i/rubyXL-addressing. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
83
85
|
|
84
86
|
## License
|
85
87
|
|
data/lib/rubyXL/address.rb
CHANGED
@@ -2,55 +2,6 @@
|
|
2
2
|
|
3
3
|
module RubyXL
|
4
4
|
class Address
|
5
|
-
ROW_REF_FORMAT = /\A[1-9]\d*\z/
|
6
|
-
COLUMN_REF_FORMAT = /\A[A-Z]+\z/
|
7
|
-
|
8
|
-
class << self
|
9
|
-
# @param [Integer] ind
|
10
|
-
# @return [String]
|
11
|
-
def row_ind2ref(ind)
|
12
|
-
message = "invalid row #{ind.inspect}"
|
13
|
-
raise TypeError, message unless ind.is_a?(Integer)
|
14
|
-
raise ArgumentError, message unless ind >= 0
|
15
|
-
|
16
|
-
(ind + 1).to_s.freeze
|
17
|
-
end
|
18
|
-
|
19
|
-
# @param [Integer] ind
|
20
|
-
# @return [String]
|
21
|
-
def column_ind2ref(ind)
|
22
|
-
message = "invalid column #{ind.inspect}"
|
23
|
-
raise TypeError, message unless ind.is_a?(Integer)
|
24
|
-
raise ArgumentError, message unless ind >= 0
|
25
|
-
|
26
|
-
ref = ''.dup
|
27
|
-
loop do
|
28
|
-
ref.prepend((ind % 26 + 65).chr)
|
29
|
-
ind /= 26
|
30
|
-
break if (ind -= 1) < 0
|
31
|
-
end
|
32
|
-
ref.freeze
|
33
|
-
end
|
34
|
-
|
35
|
-
# @param [String, Symbol] ref
|
36
|
-
# @return [Integer]
|
37
|
-
def row_ref2ind(ref)
|
38
|
-
message = "invalid row #{ref.inspect}"
|
39
|
-
raise ArgumentError, message unless ROW_REF_FORMAT =~ ref
|
40
|
-
|
41
|
-
ref.to_s.to_i - 1
|
42
|
-
end
|
43
|
-
|
44
|
-
# @param [String, Symbol] ref
|
45
|
-
# @return [Integer]
|
46
|
-
def column_ref2ind(ref)
|
47
|
-
message = "invalid column #{ref.inspect}"
|
48
|
-
raise ArgumentError, message unless COLUMN_REF_FORMAT =~ ref
|
49
|
-
|
50
|
-
ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
5
|
# @param [RubyXL::Worksheet] worksheet
|
55
6
|
# @return [RubyXL::Worksheet]
|
56
7
|
attr_writer :worksheet
|
@@ -100,27 +51,13 @@ module RubyXL
|
|
100
51
|
# @param [Integer, String, Symbol] row
|
101
52
|
# @return [Integer, String, Symbol]
|
102
53
|
def row=(row)
|
103
|
-
|
104
|
-
when Integer
|
105
|
-
return @row = row if row >= 0
|
106
|
-
when String, Symbol
|
107
|
-
return self.row = self.class.row_ref2ind(row)
|
108
|
-
end
|
109
|
-
|
110
|
-
raise ArgumentError, "invalid row #{row.inspect}"
|
54
|
+
@row = Addressing.__send__(:normalize, 'row', row)
|
111
55
|
end
|
112
56
|
|
113
57
|
# @param [Integer, String, Symbol] column
|
114
58
|
# @return [Integer, String, Symbol]
|
115
59
|
def column=(column)
|
116
|
-
|
117
|
-
when Integer
|
118
|
-
return @column = column if column >= 0
|
119
|
-
when String, Symbol
|
120
|
-
return self.column = self.class.column_ref2ind(column)
|
121
|
-
end
|
122
|
-
|
123
|
-
raise ArgumentError, "invalid column #{column.inspect}"
|
60
|
+
@column = Addressing.__send__(:normalize, 'column', column)
|
124
61
|
end
|
125
62
|
|
126
63
|
# @return [String]
|
data/lib/rubyXL/addressing.rb
CHANGED
@@ -7,6 +7,69 @@ module RubyXL
|
|
7
7
|
autoload :Address, 'rubyXL/address'
|
8
8
|
|
9
9
|
module Addressing
|
10
|
+
ROW_REF_FORMAT = /\A[1-9]\d*\z/
|
11
|
+
COLUMN_REF_FORMAT = /\A[A-Z]+\z/
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# @param [Integer] ind
|
15
|
+
# @return [String]
|
16
|
+
def row_ind2ref(ind)
|
17
|
+
validate_index('row', ind)
|
18
|
+
|
19
|
+
(ind + 1).to_s.freeze
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param [Integer] ind
|
23
|
+
# @return [String]
|
24
|
+
def column_ind2ref(ind)
|
25
|
+
validate_index('column', ind)
|
26
|
+
|
27
|
+
ref = ''.dup
|
28
|
+
loop do
|
29
|
+
ref.prepend((ind % 26 + 65).chr)
|
30
|
+
ind = ind / 26 - 1
|
31
|
+
break if ind < 0
|
32
|
+
end
|
33
|
+
ref.freeze
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param [String, Symbol] ref
|
37
|
+
# @return [Integer]
|
38
|
+
def row_ref2ind(ref)
|
39
|
+
message = "invalid row #{ref.inspect}"
|
40
|
+
raise ArgumentError, message unless ROW_REF_FORMAT =~ ref
|
41
|
+
|
42
|
+
ref.to_s.to_i - 1
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param [String, Symbol] ref
|
46
|
+
# @return [Integer]
|
47
|
+
def column_ref2ind(ref)
|
48
|
+
message = "invalid column #{ref.inspect}"
|
49
|
+
raise ArgumentError, message unless COLUMN_REF_FORMAT =~ ref
|
50
|
+
|
51
|
+
ref.to_s.each_byte.reduce(0) { |a, e| a * 26 + (e - 64) } - 1
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def normalize(row_or_column, value)
|
57
|
+
case value
|
58
|
+
when String, Symbol
|
59
|
+
value = public_send("#{row_or_column}_ref2ind", value)
|
60
|
+
else
|
61
|
+
validate_index(row_or_column, value)
|
62
|
+
end
|
63
|
+
value
|
64
|
+
end
|
65
|
+
|
66
|
+
def validate_index(row_or_column, index)
|
67
|
+
message = "invalid #{row_or_column} #{index.inspect}"
|
68
|
+
raise TypeError, message unless index.is_a?(Integer)
|
69
|
+
raise ArgumentError, message unless index >= 0
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
10
73
|
# @param [String, Symbol, Integer] ref_or_row is ref `'A1'`
|
11
74
|
# or row index `0`
|
12
75
|
# or row label `'1'`
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyXL-addressing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masaki Takeuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-03-
|
11
|
+
date: 2016-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyXL
|