Module: Timezone::Loader
Relationships & Source Files | |
Defined in: | lib/timezone/loader.rb |
Overview
Responsible for loading and parsing timezone data from files.
Constant Summary
-
SOURCE_BIT =
# File 'lib/timezone/loader.rb', line 90
-
ZONE_FILE_PATH =
# File 'lib/timezone/loader.rb', line 8File. ("#{File.dirname(__FILE__)}/../../data")
Class Method Summary
- .load(name)
- .names
- .valid?(name) ⇒ Boolean
-
.get_zone_data(name)
private
Retrieve the data from a particular time zone.
- .parse_zone_data(data) private
- .parse_zone_names private
Class Method Details
.get_zone_data(name) (private)
Retrieve the data from a particular time zone
# File 'lib/timezone/loader.rb', line 58
def get_zone_data(name) File.read(File.join(ZONE_FILE_PATH, name)) end
.load(name)
[ GitHub ]# File 'lib/timezone/loader.rb', line 14
def load(name) @rules.fetch(name) do raise ::Timezone::Error::InvalidZone unless valid?(name) @rules[name] = parse_zone_data(get_zone_data(name)) end end
.names
[ GitHub ]# File 'lib/timezone/loader.rb', line 22
def names @names ||= parse_zone_names end
.parse_zone_data(data) (private)
[ GitHub ]# File 'lib/timezone/loader.rb', line 42
def parse_zone_data(data) rules = [] data.split("\n").each do |line| source, name, dst, offset = line.split(':') source = source.to_i dst = dst == '1' offset = offset.to_i source = rules.last[SOURCE_BIT] + source if rules.last rules << [source, name, dst, offset] end rules end
.parse_zone_names (private)
[ GitHub ]# File 'lib/timezone/loader.rb', line 32
def parse_zone_names files = Dir[File.join(ZONE_FILE_PATH, '**/*')].map do |file| next if File.directory?(file) file.sub("#{ZONE_FILE_PATH}/", '') end files.compact end
.valid?(name) ⇒ Boolean
# File 'lib/timezone/loader.rb', line 26
def valid?(name) names.include?(name) end