iso_bsd-i18n 1.0.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 +7 -0
- data/.document +5 -0
- data/.gitignore +28 -0
- data/.rspec +1 -0
- data/.travis.yml +1 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +20 -0
- data/README.md +40 -0
- data/Rakefile +41 -0
- data/iso_bsd-i18n.gemspec +31 -0
- data/lib/iso_bsd-i18n.rb +18 -0
- data/lib/iso_bsd-i18n/division.rb +58 -0
- data/lib/iso_bsd-i18n/iso_bsd-i18n_initializer.rb +7 -0
- data/lib/iso_bsd-i18n/rarity.rb +130 -0
- data/lib/iso_bsd-i18n/size.rb +244 -0
- data/lib/iso_bsd-i18n/version.rb +3 -0
- data/locales/en.yml +177 -0
- data/locales/es.yml +177 -0
- data/spec/division_spec.rb +28 -0
- data/spec/iso_bsd-i18n_spec.rb +8 -0
- data/spec/rarity_spec.rb +112 -0
- data/spec/size_spec.rb +54 -0
- data/spec/spec_helper.rb +12 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ceef5f26f3ebeaf81508fb5a3728e993e5a286d6
|
4
|
+
data.tar.gz: 7e68f9db7ee9725eac6315515a4f00686674a166
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb845651fc8f40d60147b627bf39848e18e4bf06835216bc035b1ec3f87a08b5d7d0c839f7fcb4ccc150cdd5487d995b4f0edb8be3ad0040ecf4c1936583cc61
|
7
|
+
data.tar.gz: 8346cca19f8edd8357244d593ff791c0780fb29aaec8dfe44fc47f7b1e3b9a22c73ae473de5f8cc2dcecb31492b7b98f588369c2a00d0408abeb5547538c3db5
|
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
.localeapp/
|
18
|
+
|
19
|
+
*~
|
20
|
+
*.out
|
21
|
+
*.project
|
22
|
+
*.DS_Store
|
23
|
+
*~
|
24
|
+
.#*
|
25
|
+
*#
|
26
|
+
Gemfile.lock
|
27
|
+
*.lock
|
28
|
+
*.gem
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm: 1.9.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 William Wedler
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# iso_bsd-i18n
|
2
|
+
|
3
|
+
Text data for describing ISO Bead Seat Diameters for bicycle wheels.
|
4
|
+
|
5
|
+
For background, see
|
6
|
+
http://www.sheldonbrown.com/tire-sizing.html#isoetrto
|
7
|
+
|
8
|
+
## Basic Usage
|
9
|
+
|
10
|
+
require 'iso_bsd-i18n'
|
11
|
+
|
12
|
+
s = IsoBsdI18n::Size.new(622)
|
13
|
+
# Application(s)
|
14
|
+
s.app
|
15
|
+
# Traditional name(s)
|
16
|
+
s.trad
|
17
|
+
# Rarity information
|
18
|
+
s.rarity
|
19
|
+
# Test if common size
|
20
|
+
s.rarity.commmon?
|
21
|
+
|
22
|
+
# SizeCollection of all sizes
|
23
|
+
c = IsoBsdI18n::Size.all
|
24
|
+
|
25
|
+
# SizeCollection of common sizes
|
26
|
+
# (based on default rarity divisions)
|
27
|
+
c = IsoBsdI18n::Rarity::default_division.common
|
28
|
+
|
29
|
+
my_divisions = {:common=>[bsd,..], :uncommon=>[bsd,..], :rare=>[bsd]}
|
30
|
+
|
31
|
+
# Testing rarity based on specified divisions
|
32
|
+
s.rarity(my_divisions).common?
|
33
|
+
|
34
|
+
# Override default rarity divisions
|
35
|
+
Rarity::default_division = my_divisions
|
36
|
+
|
37
|
+
# License
|
38
|
+
|
39
|
+
See license file.
|
40
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
|
6
|
+
begin
|
7
|
+
Bundler.setup(:default, :development)
|
8
|
+
rescue Bundler::BundlerError => e
|
9
|
+
$stderr.puts e.message
|
10
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
11
|
+
exit e.status_code
|
12
|
+
end
|
13
|
+
require 'rake'
|
14
|
+
|
15
|
+
require 'rspec/core'
|
16
|
+
require 'rspec/core/rake_task'
|
17
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
18
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :spec
|
22
|
+
|
23
|
+
begin
|
24
|
+
# rake/rdoctask is deprecated in RDoc 2.4.2+
|
25
|
+
require 'rdoc/task'
|
26
|
+
rescue LoadError
|
27
|
+
require 'rake/rdoctask'
|
28
|
+
end
|
29
|
+
|
30
|
+
require_relative './lib/iso_bsd-i18n'
|
31
|
+
|
32
|
+
Rake::RDocTask.new do |rdoc|
|
33
|
+
version = IsoBsdI18n::VERSION
|
34
|
+
|
35
|
+
rdoc.rdoc_dir = 'rdoc'
|
36
|
+
rdoc.title = "iso_bsd-i18n #{version}"
|
37
|
+
rdoc.rdoc_files.include('README*')
|
38
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
39
|
+
end
|
40
|
+
|
41
|
+
require 'i18n-spec/tasks'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'iso_bsd-i18n/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{iso_bsd-i18n}
|
7
|
+
s.version = IsoBsdI18n::VERSION
|
8
|
+
s.homepage = "http://freeridepg.org/"
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.authors = [%q{William Wedler}]
|
11
|
+
s.date = %q{2012-11-09}
|
12
|
+
s.description = %q{Text describing bicycle wheel size information.}
|
13
|
+
s.email = %q{wedler.w@freeridepgh.org}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE.txt",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.licenses = [%q{MIT}]
|
20
|
+
s.require_paths = [%q{lib}]
|
21
|
+
s.rubygems_version = %q{1.8.6}
|
22
|
+
s.summary = %q{Text describing bicycle wheel size information. The purpose of this information is to assist in understanding and identifying a bicycle wheel size.}
|
23
|
+
|
24
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
25
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
26
|
+
s.add_development_dependency(%q<i18n-spec>, [">= 0"])
|
27
|
+
|
28
|
+
s.add_runtime_dependency 'i18n', '>= 0', '>= 0'
|
29
|
+
s.add_runtime_dependency 'bundler', '~> 1.2', '~> 1.2'
|
30
|
+
end
|
31
|
+
|
data/lib/iso_bsd-i18n.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require_relative 'iso_bsd-i18n/version'
|
3
|
+
require_relative 'iso_bsd-i18n/size'
|
4
|
+
require_relative 'iso_bsd-i18n/rarity'
|
5
|
+
|
6
|
+
module IsoBsdI18n
|
7
|
+
def IsoBsdI18n::init
|
8
|
+
I18n.load_path << Dir[File.join(File.expand_path(File.dirname(__FILE__) + '/../locales'), '*.yml')]
|
9
|
+
I18n.load_path.flatten!
|
10
|
+
end
|
11
|
+
|
12
|
+
if defined?(Rails)
|
13
|
+
require 'iso_bsd-i18n/iso_bsd-i18n_initializer.rb'
|
14
|
+
else
|
15
|
+
IsoBsdI18n::init
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module IsoBsdI18n
|
4
|
+
|
5
|
+
# A Division encapsulates the classification of wheels into
|
6
|
+
# specified non-intersecting groups.
|
7
|
+
class Division
|
8
|
+
|
9
|
+
# @param [Hash, #each_pair] group_list Hash mapping division to array of sizes in the division
|
10
|
+
def initialize(group_list)
|
11
|
+
@raw_groups = group_list
|
12
|
+
@data = DivisionData.new(@raw_groups)
|
13
|
+
self.extend @data.to_mod
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Hash]
|
17
|
+
def hash
|
18
|
+
@raw_groups
|
19
|
+
end
|
20
|
+
|
21
|
+
def == (other)
|
22
|
+
@raw_groups == other.hash
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Defining methods on the fly for Division instances
|
28
|
+
# http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html
|
29
|
+
class DivisionData
|
30
|
+
def initialize(data)
|
31
|
+
@h = data
|
32
|
+
@h ||= {}
|
33
|
+
@groups = {} # holder for caching
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_mod
|
37
|
+
h = @h
|
38
|
+
grps = @groups
|
39
|
+
Module.new do
|
40
|
+
h.each_pair do |gname, collection|
|
41
|
+
define_method gname do
|
42
|
+
col = grps[gname]
|
43
|
+
col ||= SizeCollection.new(collection)
|
44
|
+
grps[gname] = col
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method "#{gname}?" do |bsd|
|
48
|
+
col = grps[gname]
|
49
|
+
col ||= SizeCollection.new(collection)
|
50
|
+
col.include?(bsd)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end # class DivisionData
|
56
|
+
|
57
|
+
end # class Division
|
58
|
+
end # module BsdIsoI18n
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
require_relative 'size'
|
3
|
+
require_relative 'division'
|
4
|
+
|
5
|
+
module IsoBsdI18n
|
6
|
+
|
7
|
+
# Classify Size instances based on rarity.
|
8
|
+
# Used to help distinguish similar sizes.
|
9
|
+
#
|
10
|
+
module Rarity
|
11
|
+
|
12
|
+
COMMON = [630, 622, 559, 507, 406, 305] unless defined?(COMMON)
|
13
|
+
UNCOMMON = [590, 597, 571] unless defined?(UNCOMMON)
|
14
|
+
RARE = [635, 599, 587, 584, 547, 540, 520,
|
15
|
+
490, 457, 451, 440, 419, 390, 369,
|
16
|
+
355, 349, 340, 337, 317, 203, 152, 137] unless defined?(RARE)
|
17
|
+
|
18
|
+
# The Division object used to classify size rarity.
|
19
|
+
# Used by default when explicit division is not specifed
|
20
|
+
# for Rarity functions.
|
21
|
+
#
|
22
|
+
# @return [Division]
|
23
|
+
def self.default_division
|
24
|
+
@default_division ||= Division.new(self.default_division_raw)
|
25
|
+
@default_division
|
26
|
+
end
|
27
|
+
|
28
|
+
# Configure the default to specify rarity divisions.
|
29
|
+
# When nil is given, the default is defined based on
|
30
|
+
# COMMON, UNCOMMON and RARE lists.
|
31
|
+
#
|
32
|
+
# @param [Hash, Division, nil] d
|
33
|
+
def self.default_division=(d)
|
34
|
+
if d.class == Division
|
35
|
+
@default_division = d
|
36
|
+
else
|
37
|
+
d = nil unless d.respond_to?(:each_pair)
|
38
|
+
d ||= self.default_division_raw
|
39
|
+
@default_division = Division.new(d)
|
40
|
+
end
|
41
|
+
return @default_division
|
42
|
+
end
|
43
|
+
|
44
|
+
# Get the Hash to specify a default division based on
|
45
|
+
# COMMON, UNCOMMON and RARE lists
|
46
|
+
# @return [Hash]
|
47
|
+
def self.default_division_raw
|
48
|
+
{
|
49
|
+
:common => COMMON,
|
50
|
+
:uncommon => UNCOMMON,
|
51
|
+
:rare => RARE
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get the sizes of the given rarity
|
56
|
+
#
|
57
|
+
# @params [Symbol, String] rarity
|
58
|
+
# @params [Division] division
|
59
|
+
# @return [SizeCollection]
|
60
|
+
def self.get(rarity, division=nil)
|
61
|
+
division ||= default_division
|
62
|
+
division.send(rarity)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Rarity value object to be used as an attribute to Size instances
|
66
|
+
#
|
67
|
+
class Value
|
68
|
+
|
69
|
+
# @param [Integer] bsd
|
70
|
+
# @param [Division, #each_pair] division rarity classifications
|
71
|
+
def initialize(bsd, division=nil)
|
72
|
+
@bsd = bsd
|
73
|
+
self.division=division
|
74
|
+
@data = ValueData.new(@bsd, @division)
|
75
|
+
self.extend @data.to_mod
|
76
|
+
end
|
77
|
+
|
78
|
+
# @return [String] Translated rarity
|
79
|
+
def to_s
|
80
|
+
str = nil
|
81
|
+
@division.hash.keys.each do |k|
|
82
|
+
if self.send("#{k}?")
|
83
|
+
str = I18n.translate("isobsd.rarity.#{k}")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
str ||= AttribNoData.new
|
87
|
+
|
88
|
+
return str
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
|
93
|
+
# Initialize the Division object used to test for rarity
|
94
|
+
#
|
95
|
+
def division=(div)
|
96
|
+
@division = div if div.class == Division
|
97
|
+
@division ||= Division.new(div) if div.respond_to?(:each_pair)
|
98
|
+
@division ||= Rarity::default_division
|
99
|
+
end
|
100
|
+
|
101
|
+
# Defining methods on the fly for Value instances
|
102
|
+
class ValueData
|
103
|
+
def initialize(bsd, division)
|
104
|
+
@diameter = bsd
|
105
|
+
@div = division
|
106
|
+
@membership = {}
|
107
|
+
end
|
108
|
+
|
109
|
+
# @return [Module] A Value instance extends on initialization
|
110
|
+
def to_mod
|
111
|
+
div = @div
|
112
|
+
h = div.hash
|
113
|
+
diam = @diameter
|
114
|
+
mem = @membership
|
115
|
+
Module.new do
|
116
|
+
h.each_pair do |gname, collection|
|
117
|
+
define_method "#{gname}?" do
|
118
|
+
mem[gname] ||= div.send("#{gname}?", diam)
|
119
|
+
mem[gname]
|
120
|
+
end
|
121
|
+
end # h.each_pair do
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end # class ValueData
|
125
|
+
|
126
|
+
end # class Value
|
127
|
+
|
128
|
+
end # module Rarity
|
129
|
+
|
130
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
require 'i18n'
|
2
|
+
|
3
|
+
module IsoBsdI18n
|
4
|
+
|
5
|
+
class AttribNoData < String
|
6
|
+
def initialize(str = nil)
|
7
|
+
str ||= I18n.translate('isobsd.messages.not_applicable')
|
8
|
+
super(str)
|
9
|
+
end
|
10
|
+
|
11
|
+
def capitalize
|
12
|
+
self.to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class SizeUnknown < String
|
17
|
+
def initialize(bsd)
|
18
|
+
@bsd = bsd
|
19
|
+
super(I18n.translate('isobsd.messages.unknown'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Size
|
24
|
+
|
25
|
+
# @param [Integer] bsd
|
26
|
+
#
|
27
|
+
def initialize(bsd)
|
28
|
+
@bsd = bsd
|
29
|
+
end
|
30
|
+
|
31
|
+
# Test if the given BSD value maps to known size data
|
32
|
+
#
|
33
|
+
# @param [Integer, #to_i] val
|
34
|
+
#
|
35
|
+
# @return [Boolean]
|
36
|
+
def self.unknown?(val)
|
37
|
+
!I18n.translate('isobsd.sizes').has_key?(val.to_i)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Test if this size is unknown
|
41
|
+
#
|
42
|
+
# @return [Boolean]
|
43
|
+
def unknown?
|
44
|
+
Size.unknown?(@bsd)
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [String] The bsd as string.
|
48
|
+
#
|
49
|
+
def to_s
|
50
|
+
@bsd.to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_sym
|
54
|
+
@bsd.to_s.to_sym
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
# @return [Integer] The bsd for this size.
|
59
|
+
#
|
60
|
+
def to_i
|
61
|
+
@bsd.to_i
|
62
|
+
end
|
63
|
+
|
64
|
+
def <=>(other)
|
65
|
+
self.to_i <=> other.to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [String] The bsd and units like 622mm
|
69
|
+
#
|
70
|
+
def diameter
|
71
|
+
@diam ||= "#{@bsd}mm"
|
72
|
+
@diam
|
73
|
+
end
|
74
|
+
|
75
|
+
# Traditional name or names of this size
|
76
|
+
# @return [String, Array]
|
77
|
+
def trad
|
78
|
+
lookup(:trad)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Appliication(s) of this size
|
82
|
+
# @return [String, Array]
|
83
|
+
def app
|
84
|
+
lookup(:app)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Rarity of the size given rarity divisions
|
88
|
+
#
|
89
|
+
# @param [Division, #each_pair] rarity_data rarity classifications
|
90
|
+
#
|
91
|
+
def rarity(rarity_data = nil)
|
92
|
+
if rarity_data.nil?
|
93
|
+
# Only cache when default rarity being used
|
94
|
+
@rarity ||= Rarity::Value.new(@bsd, rarity_data)
|
95
|
+
return @rarity
|
96
|
+
else
|
97
|
+
return Rarity::Value.new(@bsd, rarity_data)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Provides a hash containing all attributes
|
102
|
+
#
|
103
|
+
# @return [Hash] of size attributes
|
104
|
+
def hash
|
105
|
+
{
|
106
|
+
:diameter => self.diameter,
|
107
|
+
:app => self.app,
|
108
|
+
:trad => self.trad,
|
109
|
+
:rarity => self.rarity.to_s
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
# @return [Array] All size information in an Array of Size objects
|
114
|
+
def Size.all
|
115
|
+
@all ||= SizeCollection.new(I18n.translate('isobsd.sizes').keys)
|
116
|
+
@all
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
# Lookup the attribute for the given key
|
122
|
+
# Returns an Array if multiple values map the the attribute
|
123
|
+
#
|
124
|
+
# @param [Symbol] key Attribute to lookup
|
125
|
+
# @return [String, Array]
|
126
|
+
def lookup(key)
|
127
|
+
if unknown?
|
128
|
+
return data
|
129
|
+
end
|
130
|
+
|
131
|
+
val = data[key]
|
132
|
+
val ||= AttribNoData.new
|
133
|
+
|
134
|
+
# Convert multi-lined entries to an array
|
135
|
+
if val.class == Hash
|
136
|
+
val = val.values
|
137
|
+
end
|
138
|
+
|
139
|
+
return val
|
140
|
+
end
|
141
|
+
|
142
|
+
# Wrapper for the I18n::translate call to retrieve wheel size data
|
143
|
+
# @return [Hash]
|
144
|
+
def data
|
145
|
+
@data ||= (unknown?) ? SizeUnknown.new(@bsd) : I18n.translate("isobsd.sizes")[@bsd]
|
146
|
+
@data
|
147
|
+
end
|
148
|
+
|
149
|
+
end # class Size
|
150
|
+
|
151
|
+
class SizeCollection
|
152
|
+
|
153
|
+
include Enumerable
|
154
|
+
|
155
|
+
# @param [Array] data List of sizes to include in the collection
|
156
|
+
def initialize(data=[])
|
157
|
+
@list = data
|
158
|
+
end
|
159
|
+
|
160
|
+
# Array of size objects in the collection
|
161
|
+
#
|
162
|
+
# @return [Array]
|
163
|
+
def sizes
|
164
|
+
select_sizes(@list)
|
165
|
+
end
|
166
|
+
|
167
|
+
def each
|
168
|
+
self.sizes.each do |s|
|
169
|
+
yield s
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Test if the given size is in the collection
|
174
|
+
#
|
175
|
+
# @param [Size, #==] val
|
176
|
+
#
|
177
|
+
# @return [Boolean]
|
178
|
+
def include?(val)
|
179
|
+
@list.include?(val)
|
180
|
+
end
|
181
|
+
|
182
|
+
# A nested hash of structure
|
183
|
+
# { size.diameter => size.hash ,... }
|
184
|
+
#
|
185
|
+
# @return (Hash)
|
186
|
+
def hash
|
187
|
+
Hash[sizes.map{|s| [s.to_i, s.hash]}]
|
188
|
+
end
|
189
|
+
|
190
|
+
# @param [Array] locales String array specifying locales to include
|
191
|
+
# @return [Hash] The hash for each Size in the collection for each locale specified
|
192
|
+
def hash_locale(locales=nil)
|
193
|
+
orig_locale = I18n.locale
|
194
|
+
h = {}
|
195
|
+
|
196
|
+
if(locales.nil? || locales.empty?)
|
197
|
+
locales = I18n::available_locales
|
198
|
+
end
|
199
|
+
|
200
|
+
begin
|
201
|
+
locales.each do |l|
|
202
|
+
begin
|
203
|
+
if ! I18n.translate('isobsd', :raise => true, :locale => l).nil?
|
204
|
+
I18n.locale = l
|
205
|
+
h[l] = self.hash
|
206
|
+
end
|
207
|
+
rescue
|
208
|
+
end
|
209
|
+
end
|
210
|
+
ensure
|
211
|
+
I18n.locale = orig_locale
|
212
|
+
end
|
213
|
+
return h
|
214
|
+
end
|
215
|
+
|
216
|
+
# Hash of structure
|
217
|
+
# {size.bsd => size.diameter, ... }
|
218
|
+
#
|
219
|
+
# @return [Hash]
|
220
|
+
def labels
|
221
|
+
Hash[sizes.map{|s| [s.to_s, s.diameter]}]
|
222
|
+
end
|
223
|
+
|
224
|
+
# Array of size.hash
|
225
|
+
#
|
226
|
+
# @return [Array]
|
227
|
+
def to_data
|
228
|
+
sizes.map{|s| {:id=>s.to_s, :text=>s.diameter}}
|
229
|
+
end
|
230
|
+
|
231
|
+
private
|
232
|
+
|
233
|
+
# @param [Array] targets List of sizes to include
|
234
|
+
# @param [Hash] size_data Data set to build Size objects from
|
235
|
+
# @return [Array] of Size objects
|
236
|
+
def select_sizes(targets=nil,size_data=nil)
|
237
|
+
size_data ||= I18n.translate('isobsd.sizes')
|
238
|
+
group = size_data.select{|k,v| (targets.include? k)} unless targets.nil?
|
239
|
+
group ||= size_data
|
240
|
+
group.keys.map{|k| Size.new(k)}
|
241
|
+
end
|
242
|
+
end # class SizeCollection
|
243
|
+
|
244
|
+
end
|