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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a251d2a6809a53c0336e2f33fb94fe7f323a946a
4
- data.tar.gz: 6456b655fb52580ef39afca0b0bcb2c91e4047df
3
+ metadata.gz: 0631645fffcb1b80f72b10339b3150d1e1b5e187
4
+ data.tar.gz: e66ca645218f47ace24fa1341205807f68ecf9e9
5
5
  SHA512:
6
- metadata.gz: 3b7e5352f1a0e0630932b0f4e9683754db30d5c66cb649f9bdd03d6a47ca5183a7775634f75fcce00862b9e7875da001b8933525d5dc1dfbbac3c073d355d06f
7
- data.tar.gz: e62d77e745c40b33311abe10c46c486f78579c81f91147b1107e954af432aa708b85c0e347a1b9ab09b575ca65ba9702182b3ea0f83946021868b5325d064450
6
+ metadata.gz: 37ce540e93dda95bc1f9faef6f36b1214bbb817d74c92de09fb12184ad0e0cbb139026c92c5331e3d197a25d4f26f7d5d0d3651a0a6c0119738b2721b222dcbf
7
+ data.tar.gz: a4b54aa278663be11b1ca8aa356431da95aed9db3f933262b102181d248154e3d1a0059c48845169bb55e408e4646115463151e11661a844d61dcfce5591120d
data/.rubocop.yml CHANGED
@@ -12,7 +12,6 @@ Metrics/AbcSize:
12
12
 
13
13
  Metrics/ClassLength:
14
14
  Exclude:
15
- - lib/rubyXL/address.rb
16
15
  - test/**/*_test.rb
17
16
 
18
17
  Metrics/LineLength:
data/README.md CHANGED
@@ -2,8 +2,10 @@
2
2
 
3
3
  Addressing for [rubyXL](https://github.com/weshatheleopard/rubyXL)
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/rubyXL-addressing.svg)](https://badge.fury.io/rb/rubyXL-addressing)
5
6
  [![Build Status](https://travis-ci.org/m4i/rubyXL-addressing.svg?branch=master)](https://travis-ci.org/m4i/rubyXL-addressing)
6
7
  [![Test Coverage](https://codeclimate.com/github/m4i/rubyXL-addressing/badges/coverage.svg)](https://codeclimate.com/github/m4i/rubyXL-addressing/coverage)
8
+ [![Code Climate](https://codeclimate.com/github/m4i/rubyXL-addressing/badges/gpa.svg)](https://codeclimate.com/github/m4i/rubyXL-addressing)
7
9
  [![Dependency Status](https://gemnasium.com/m4i/rubyXL-addressing.svg)](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/[USERNAME]/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.
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
 
@@ -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
- case row
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
- case column
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]
@@ -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'`
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RubyXL
4
4
  module Addressing
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
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.1.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-19 00:00:00.000000000 Z
11
+ date: 2016-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyXL