Class: FiasReader::Cache::Guid
- Inherits:
-
Object
- Object
- FiasReader::Cache::Guid
- Defined in:
- lib/fias_reader/cache/guid.rb
Constant Summary collapse
- DASH_STR =
'-'.freeze
- EMPTY_STR =
''.freeze
- GUID_PARTS =
[ 0..8, 8..16, 16..24, 24..32 ].freeze
- GUID_FORMAT_STR =
'%08x'.freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
- #init_from_array(val) ⇒ Object
- #init_from_guid(guid) ⇒ Object
- #init_from_hash(hash) ⇒ Object
- #init_from_hash_slice(hash, mask) ⇒ Object
-
#initialize(*args) ⇒ Guid
constructor
A new instance of Guid.
- #to_attr(name) ⇒ Object
- #to_guid ⇒ Object
Constructor Details
#initialize(*args) ⇒ Guid
Returns a new instance of Guid.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fias_reader/cache/guid.rb', line 13 def initialize(*args) if args.size == 1 guid = args[0] case guid when Hash init_from_hash guid when Array init_from_array guid else init_from_guid guid end elsif args.size == 2 init_from_hash_slice args[0], args[1] elsif args.size > 2 init_from_array args end end |
Instance Attribute Details
#parts ⇒ Object
43 44 45 |
# File 'lib/fias_reader/cache/guid.rb', line 43 def parts @parts ||= [] end |
Instance Method Details
#init_from_array(val) ⇒ Object
49 50 51 |
# File 'lib/fias_reader/cache/guid.rb', line 49 def init_from_array(val) self.parts = val end |
#init_from_guid(guid) ⇒ Object
53 54 55 56 57 |
# File 'lib/fias_reader/cache/guid.rb', line 53 def init_from_guid(guid) (self.parts = []) && return unless guid guid.tr!(DASH_STR, EMPTY_STR) self.parts = GUID_PARTS.map { |range| guid[range].to_i(16) } end |
#init_from_hash(hash) ⇒ Object
59 60 61 |
# File 'lib/fias_reader/cache/guid.rb', line 59 def init_from_hash(hash) hash.each { |key, val| parts[key[-1].to_i] = val } end |
#init_from_hash_slice(hash, mask) ⇒ Object
63 64 65 66 67 |
# File 'lib/fias_reader/cache/guid.rb', line 63 def init_from_hash_slice(hash, mask) self.parts = hash.keys.select { |key| key.to_s =~ /^#{mask}\d/ }.each_with_object([]) do |key, obj| obj[key[-1].to_i] = hash[key] end end |
#to_attr(name) ⇒ Object
32 33 34 35 36 |
# File 'lib/fias_reader/cache/guid.rb', line 32 def to_attr(name) @parts.each_with_index.each_with_object({}) do |(part, index), result| result["#{name}#{index}"] = part end end |
#to_guid ⇒ Object
39 40 41 |
# File 'lib/fias_reader/cache/guid.rb', line 39 def to_guid @parts.each_with_object('') { |i, str| str << format(GUID_FORMAT_STR, i) } end |