Sha256: 4a0431a37267cd40508821373b60d3eb80ca1fa18eca3fac59bdf959bccf9ee7

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Shared
    class Resources

      def get_resource(*path)
        resources_cache[resource_file_path(path)]
      end

      def get_locale_resource(locale, resource_name)
        get_resource(:locales, TwitterCldr.convert_locale(locale), resource_name)
      end

      private

      def resources_cache
        @resources_cache ||= Hash.new { |hash, path| hash[path] = load_resource(path) }
      end

      def resource_file_path(path)
        "#{File.join(*path.map(&:to_s))}.yml"
      end

      def load_resource(path)
        TwitterCldr::Utils.deep_symbolize_keys(YAML.load(read_resource_file(path)))
      end

      def read_resource_file(path)
        file_path = File.join(TwitterCldr::RESOURCES_DIR, path)

        if File.file?(file_path)
          File.read(file_path)
        else
          raise ArgumentError.new("Resource '#{path}' not found.")
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter_cldr-1.5.0 lib/twitter_cldr/shared/resources.rb
twitter_cldr-1.4.1 lib/twitter_cldr/shared/resources.rb
twitter_cldr-1.4.0 lib/twitter_cldr/shared/resources.rb
twitter_cldr-1.3.6 lib/twitter_cldr/shared/resources.rb
twitter_cldr-1.3.0 lib/twitter_cldr/shared/resources.rb