class FiasReader::Cache::Guid

Constants

DASH_STR
EMPTY_STR
GUID_FORMAT_STR
GUID_PARTS

Attributes

parts[W]

Public Class Methods

new(*args) click to toggle source
# 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

Public Instance Methods

init_from_array(val) click to toggle source
# File lib/fias_reader/cache/guid.rb, line 49
def init_from_array(val)
  self.parts = val
end
init_from_guid(guid) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
parts() click to toggle source
# File lib/fias_reader/cache/guid.rb, line 43
def parts
  @parts ||= []
end
to_attr(name) click to toggle source
# 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() click to toggle source
# 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