Class: GRid
- Inherits:
-
Object
- Object
- GRid
- Defined in:
- lib/grid.rb
Constant Summary collapse
- VERSION =
"0.0.2".freeze
- DEFAULT_ID_SCHEME =
"A1".freeze
- CHECK_CODES =
(("0".."9").to_a + ("A".."Z").to_a).freeze
Instance Attribute Summary collapse
-
#check_character ⇒ Object
readonly
Returns the value of attribute check_character.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id_scheme ⇒ Object
readonly
Returns the value of attribute id_scheme.
-
#issuer_code ⇒ Object
readonly
Returns the value of attribute issuer_code.
-
#release_number ⇒ Object
readonly
Returns the value of attribute release_number.
Class Method Summary collapse
-
.default_issuer_code ⇒ Object
The issuer code to use when instansating an instance and none is provided.
- .default_issuer_code=(code) ⇒ Object
-
.parse(str) ⇒ Object
Create an instance from
str.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#formatted ⇒ Object
Return a formatted
Stringrepresentation of the GRid. - #hash ⇒ Object
-
#initialize(parts = nil) ⇒ GRid
constructor
Create a new instance consisting of
parts. -
#recalculate_check_character! ⇒ Object
Recalculate the GRid’s check character.
-
#to_h ⇒ Object
Create a Hash representing the GRid.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Validate the GRid.
Constructor Details
#initialize(parts = nil) ⇒ GRid
Create a new instance consisting of parts. The #check_character will be calculated if none is given.
Arguments
- parts (Hash)
-
Parts of the GRid, optional
Parts
All parts are optional.
- :id_scheme (String)
-
ID scheme portion, defaults to
A1 - :issuer_code (String)
-
Issuer code portion, defaults to default_issuer_code
- :release_number (String)
-
Release number portion
- :check_character (String)
-
Check character portion, will be calculated if not given
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/grid.rb', line 86 def initialize(parts = nil) parts ||= {} @issuer_code = parts[:issuer_code] @issuer_code = self.class.default_issuer_code.dup if @issuer_code.nil? && self.class.default_issuer_code @release_number = parts[:release_number] @id_scheme = parts[:id_scheme] || DEFAULT_ID_SCHEME.dup @check_character = parts[:check_character] || calculate_check_character @errors = {} end |
Instance Attribute Details
#check_character ⇒ Object (readonly)
Returns the value of attribute check_character.
7 8 9 |
# File 'lib/grid.rb', line 7 def check_character @check_character end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/grid.rb', line 7 def errors @errors end |
#id_scheme ⇒ Object (readonly)
Returns the value of attribute id_scheme.
7 8 9 |
# File 'lib/grid.rb', line 7 def id_scheme @id_scheme end |
#issuer_code ⇒ Object (readonly)
Returns the value of attribute issuer_code.
7 8 9 |
# File 'lib/grid.rb', line 7 def issuer_code @issuer_code end |
#release_number ⇒ Object (readonly)
Returns the value of attribute release_number.
7 8 9 |
# File 'lib/grid.rb', line 7 def release_number @release_number end |
Class Method Details
.default_issuer_code ⇒ Object
The issuer code to use when instansating an instance and none is provided.
13 14 15 |
# File 'lib/grid.rb', line 13 def default_issuer_code @default_issuer_code end |
.default_issuer_code=(code) ⇒ Object
17 18 19 |
# File 'lib/grid.rb', line 17 def default_issuer_code=(code) @default_issuer_code = code end |
.parse(str) ⇒ Object
Create an instance from str.
grid = GRid.parse("A12425GABC1234002")
grid = GRid.parse("A1-2425G-ABC1234002-M")
grid = GRid.parse("grid:A1-2425G-ABC1234002-M")
Arguments
- str (String)
-
The GRid to parse, does not have to be valid
Returns
An instance of GRid representing str.
Errors
No errors are raised. A GRid is always returned. To determine if it’s valid call its #valid? method.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/grid.rb', line 41 def parse(str) return GRid.new if str.nil? str = str.strip return GRid.new if str.empty? str.upcase! str.sub!(/\AGRID:/, "") extract = lambda do |i, j| val = str[i, j] return unless val # 2.5 has delete_prefix str[i, j] = "" str.sub!(/\A[-\s+]/, "") val end GRid.new(:id_scheme => extract[0, 2], :issuer_code => extract[0, 5], :release_number => extract[0, 10], :check_character => extract[-1, 1]) end |
Instance Method Details
#==(other) ⇒ Object
158 159 160 161 |
# File 'lib/grid.rb', line 158 def ==(other) other = self.class.parse(other) if other.is_a?(String) eql?(other) end |
#eql?(other) ⇒ Boolean
163 164 165 |
# File 'lib/grid.rb', line 163 def eql?(other) other.instance_of?(self.class) && to_s == other.to_s end |
#formatted ⇒ Object
Return a formatted String representation of the GRid.
150 151 152 |
# File 'lib/grid.rb', line 150 def formatted [ id_scheme, issuer_code, release_number, check_character ].compact.join("-").upcase end |
#hash ⇒ Object
167 168 169 |
# File 'lib/grid.rb', line 167 def hash to_s.hash end |
#recalculate_check_character! ⇒ Object
Recalculate the GRid’s check character. This is only necessary when an invalid check character is provided to the instance.
132 133 134 135 |
# File 'lib/grid.rb', line 132 def recalculate_check_character! @check_character = calculate_check_character nil end |
#to_h ⇒ Object
Create a Hash representing the GRid. Keys are Symbols representing the portions of the GRid.
140 141 142 143 144 145 |
# File 'lib/grid.rb', line 140 def to_h { :id_scheme => id_scheme, :issuer_code => issuer_code, :release_number => release_number, :check_character => check_character } end |
#to_s ⇒ Object
154 155 156 |
# File 'lib/grid.rb', line 154 def to_s [ id_scheme, issuer_code, release_number, check_character ].compact.join("").upcase end |
#valid? ⇒ Boolean
Validate the GRid. To get the errors call #errors.
if !grid.valid?
grid.errors.each do |attr, errors|
printf "%10s: %s", attr, errors.join(", ")
end
end
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/grid.rb', line 117 def valid? errors.clear validate_id_scheme! validate_issuer_code! validate_release_number! validate_check_character! errors.empty? end |