File: subdivision.rb

package info (click to toggle)
ruby-countries 4.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 11,196 kB
  • sloc: ruby: 2,085; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Sources
  module Local
    # Auxiliary Subdivision class to support loading the local subdivision data to be updated with Unicode CLDR data
    class Subdivision
      attr_reader :code
      def initialize(code)
        @code = code
      end

      def load
        if File.exist?(file_path)
          YAML.load_file(file_path) || {}
        else
          {}
        end
      end

      def save(data)
        File.open(file_path, 'w') { |f| f.write data.to_yaml }
      rescue
        puts "failed to read #{file}: #{$ERROR_INFO}"
      end

      def file_path
        "lib/countries/data/subdivisions/#{code}.yaml"
      end

      def self.load(code)
        new(code).load
      end
    end
  end
end