tins 1.11.0 → 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6415329fd693754648bcb8286b4e374e0f6f97e
4
- data.tar.gz: b94c700c21e4b0c75b8e80f2a281737b197509ce
3
+ metadata.gz: e021337aa4ed20c3a4923e4ea6e4a1ed98d3318f
4
+ data.tar.gz: d37b9e4a07a0af742021d28ed2e8ac1961bc12c2
5
5
  SHA512:
6
- metadata.gz: 448317d7bffd06d6ce7e86eb3ffc8db5e4c7780e51ff3b20169bfc487e08d07a878aac4f3d290d9169cdfed8afae7b35d5dbc3dd9e52428996518727e1620c84
7
- data.tar.gz: 057feb9fc08db54c5ef9bf3063ae4f1cdf8ff0a74d4d3aeac7ce100adc97590d7054a219386db11e01b3b362e09f219ea068f0b1971f718bc26e8b3c7ed8107b
6
+ metadata.gz: 0be134266c51650540a04d94fc26babe6f607970e7c6536375d2649019a5cb8cf5c891f731869eb22f244bb78551e11e0f77b623798da949686fa449758fb73a
7
+ data.tar.gz: f7a896763c0a0f73926b251d5fd305101b53e2a220fa8bfafddcce2bd653c7ae9d19d9173e744d0b894cdf98cb7790306cbbec14382085d5989d30a6d1e70a60
@@ -6,9 +6,6 @@ rvm:
6
6
  - ruby-head
7
7
  - rbx-head
8
8
  matrix:
9
- include:
10
- - rvm: jruby
11
- env: JRUBY_OPTS="--2.0"
12
9
  allow_failures:
13
10
  - rvm: ruby-head
14
11
  - rvm: rbx-head
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.11.0
1
+ 1.12.0
@@ -16,6 +16,8 @@ module Tins::Unit
16
16
  '', 'm', 'µ', 'n', 'p', 'f', 'a', 'z', 'y',
17
17
  ].each_with_index.map { |n, i| Prefix.new(n.freeze, 1000, 1000 ** -i, true) }.freeze
18
18
 
19
+ class ParserError < ArgumentError; end
20
+
19
21
  module_function
20
22
 
21
23
  def prefixes(identifier)
@@ -34,7 +36,7 @@ module Tins::Unit
34
36
  def format(value, format: '%f %U', prefix: 1024, unit: ?b)
35
37
  prefixes = prefixes(prefix)
36
38
  first_prefix = prefixes.first or
37
- raise ArgumentError, 'a non-empty of prefixes is required'
39
+ raise ArgumentError, 'a non-empty array of prefixes is required'
38
40
  prefix = prefixes[
39
41
  (first_prefix.fraction ? -1 : 1) * Math.log(value) / Math.log(first_prefix.step)
40
42
  ]
@@ -89,17 +91,15 @@ module Tins::Unit
89
91
 
90
92
  def scan_unit
91
93
  case
92
- when unit = scan(@unit_re)
94
+ when scan(@unit_re)
93
95
  prefix = @unit_re.prefixes.find { |pre| pre.name == self[1] } or return
94
96
  @number *= prefix.multiplier
95
- when unit = scan(@unit_lc_re)
97
+ when scan(@unit_lc_re)
96
98
  prefix = @unit_lc_re.prefixes.find { |pre| pre.name == self[1] } or return
97
99
  @number *= prefix.multiplier
98
- when unit = scan(@unit_uc_re)
100
+ when scan(@unit_uc_re)
99
101
  prefix = @unit_uc_re.prefixes.find { |pre| pre.name == self[1] } or return
100
102
  @number *= prefix.multiplier
101
- else
102
- return
103
103
  end
104
104
  end
105
105
 
@@ -135,21 +135,21 @@ module Tins::Unit
135
135
  case
136
136
  when scan(/%f/)
137
137
  @unit_parser.scan_number or
138
- raise ArgumentError, "\"%f\" expected at #{location}"
138
+ raise ParserError, "\"%f\" expected at #{location}"
139
139
  when scan(/%U/)
140
140
  @unit_parser.scan_unit or
141
- raise ArgumentError, "\"%U\" expected at #{location}"
141
+ raise ParserError, "\"%U\" expected at #{location}"
142
142
  when scan(/%%/)
143
143
  @unit_parser.scan_char(?%) or
144
- raise ArgumentError, "#{?%.inspect} expected at #{location}"
144
+ raise ParserError, "#{?%.inspect} expected at #{location}"
145
145
  else
146
146
  char = scan(/./)
147
147
  @unit_parser.scan_char(char) or
148
- raise ArgumentError, "#{char.inspect} expected at #{location}"
148
+ raise ParserError, "#{char.inspect} expected at #{location}"
149
149
  end
150
150
  end
151
151
  unless eos? && @unit_parser.eos?
152
- raise ArgumentError,
152
+ raise ParserError,
153
153
  "format #{string.inspect} and string "\
154
154
  "#{@unit_parser.string.inspect} do not match"
155
155
  end
@@ -157,8 +157,16 @@ module Tins::Unit
157
157
  end
158
158
  end
159
159
 
160
+ # Parse the string +string+ if it matches +format+ with the unit +unit+ and
161
+ # the prefixes specified by +prefix+.
160
162
  def parse(string, format: '%f %U', unit: ?b, prefix: nil)
161
163
  prefixes = prefixes(prefix)
162
164
  FormatParser.new(format, UnitParser.new(string, unit, prefixes)).parse
163
165
  end
166
+
167
+ def parse?(string, **options)
168
+ parse(string, **options)
169
+ rescue ParserError
170
+ nil
171
+ end
164
172
  end
@@ -1,6 +1,6 @@
1
1
  module Tins
2
2
  # Tins version
3
- VERSION = '1.11.0'
3
+ VERSION = '1.12.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -41,12 +41,17 @@ module Tins
41
41
  assert_in_delta 17_301_504, parse('16.5 Mb').to_i, 1e-5
42
42
  assert_in_delta 16_500_000, parse('16.5 mbps', unit: 'bps').to_i, 1e-5
43
43
  assert_in_delta 0.1234e-5, parse('1.234 µS', unit: ?S, prefix: :f).to_s, 1e-5
44
- assert_raise(ArgumentError) { parse('16.5 nix', unit: ?b) }
45
- assert_raise(ArgumentError) { parse('nix Mb') }
44
+ assert_raise(ParserError) { parse('16.5 nix', unit: ?b) }
45
+ assert_raise(ParserError) { parse('nix Mb') }
46
46
  assert_in_delta 17_301_504, parse('16.5 % Mb', format: '%f %% %U').to_i, 1e-5
47
- assert_raise(ArgumentError) { parse('16.5 Mb', format: '%f %% %U') }
48
- assert_raise(ArgumentError) { parse('16.5 Mb foo', format: '%f %U') }
49
- assert_raise(ArgumentError) { parse('16.5 Mb', format: '%f %U foo') }
47
+ assert_raise(ParserError) { parse('16.5 Mb', format: '%f %% %U') }
48
+ assert_raise(ParserError) { parse('16.5 Mb foo', format: '%f %U') }
49
+ assert_raise(ParserError) { parse('16.5 Mb', format: '%f %U foo') }
50
+ end
51
+
52
+ def test_parse_predicate
53
+ assert_in_delta 17_301_504, parse?('16.5 Mb').to_i, 1e-5
54
+ assert_nil parse?('16.5 nix', unit: ?b)
50
55
  end
51
56
  end
52
57
  end
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: tins 1.11.0 ruby lib
2
+ # stub: tins 1.12.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "tins"
6
- s.version = "1.11.0"
6
+ s.version = "1.12.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Florian Frank"]
11
- s.date = "2016-07-12"
11
+ s.date = "2016-08-04"
12
12
  s.description = "All the stuff that isn't good/big enough for a real library."
13
13
  s.email = "flori@ping.de"
14
14
  s.extra_rdoc_files = ["README.md", "lib/dslkit.rb", "lib/dslkit/polite.rb", "lib/dslkit/rude.rb", "lib/spruz.rb", "lib/tins.rb", "lib/tins/alias.rb", "lib/tins/annotate.rb", "lib/tins/ask_and_send.rb", "lib/tins/attempt.rb", "lib/tins/bijection.rb", "lib/tins/case_predicate.rb", "lib/tins/complete.rb", "lib/tins/concern.rb", "lib/tins/count_by.rb", "lib/tins/date_dummy.rb", "lib/tins/date_time_dummy.rb", "lib/tins/deep_const_get.rb", "lib/tins/deep_dup.rb", "lib/tins/dslkit.rb", "lib/tins/duration.rb", "lib/tins/extract_last_argument_options.rb", "lib/tins/file_binary.rb", "lib/tins/find.rb", "lib/tins/generator.rb", "lib/tins/go.rb", "lib/tins/hash_symbolize_keys_recursive.rb", "lib/tins/hash_union.rb", "lib/tins/if_predicate.rb", "lib/tins/implement.rb", "lib/tins/limited.rb", "lib/tins/lines_file.rb", "lib/tins/memoize.rb", "lib/tins/method_description.rb", "lib/tins/minimize.rb", "lib/tins/module_group.rb", "lib/tins/named_set.rb", "lib/tins/null.rb", "lib/tins/once.rb", "lib/tins/p.rb", "lib/tins/partial_application.rb", "lib/tins/proc_compose.rb", "lib/tins/proc_prelude.rb", "lib/tins/range_plus.rb", "lib/tins/require_maybe.rb", "lib/tins/responding.rb", "lib/tins/secure_write.rb", "lib/tins/sexy_singleton.rb", "lib/tins/string_byte_order_mark.rb", "lib/tins/string_camelize.rb", "lib/tins/string_underscore.rb", "lib/tins/string_version.rb", "lib/tins/subhash.rb", "lib/tins/terminal.rb", "lib/tins/thread_local.rb", "lib/tins/time_dummy.rb", "lib/tins/to.rb", "lib/tins/to_proc.rb", "lib/tins/token.rb", "lib/tins/uniq_by.rb", "lib/tins/unit.rb", "lib/tins/version.rb", "lib/tins/write.rb", "lib/tins/xt.rb", "lib/tins/xt/annotate.rb", "lib/tins/xt/ask_and_send.rb", "lib/tins/xt/attempt.rb", "lib/tins/xt/blank.rb", "lib/tins/xt/case_predicate.rb", "lib/tins/xt/complete.rb", "lib/tins/xt/concern.rb", "lib/tins/xt/count_by.rb", "lib/tins/xt/date_dummy.rb", "lib/tins/xt/date_time_dummy.rb", "lib/tins/xt/deep_const_get.rb", "lib/tins/xt/deep_dup.rb", "lib/tins/xt/dslkit.rb", "lib/tins/xt/extract_last_argument_options.rb", "lib/tins/xt/file_binary.rb", "lib/tins/xt/full.rb", "lib/tins/xt/hash_symbolize_keys_recursive.rb", "lib/tins/xt/hash_union.rb", "lib/tins/xt/if_predicate.rb", "lib/tins/xt/implement.rb", "lib/tins/xt/irb.rb", "lib/tins/xt/method_description.rb", "lib/tins/xt/named.rb", "lib/tins/xt/null.rb", "lib/tins/xt/p.rb", "lib/tins/xt/partial_application.rb", "lib/tins/xt/proc_compose.rb", "lib/tins/xt/proc_prelude.rb", "lib/tins/xt/range_plus.rb", "lib/tins/xt/require_maybe.rb", "lib/tins/xt/responding.rb", "lib/tins/xt/secure_write.rb", "lib/tins/xt/sexy_singleton.rb", "lib/tins/xt/string.rb", "lib/tins/xt/string_byte_order_mark.rb", "lib/tins/xt/string_camelize.rb", "lib/tins/xt/string_underscore.rb", "lib/tins/xt/string_version.rb", "lib/tins/xt/subhash.rb", "lib/tins/xt/time_dummy.rb", "lib/tins/xt/time_freezer.rb", "lib/tins/xt/to.rb", "lib/tins/xt/uniq_by.rb", "lib/tins/xt/write.rb"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tins
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-12 00:00:00.000000000 Z
11
+ date: 2016-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar