Sha256: 90ebc4c364e60b62ae7809f1afa8a2f4c4c1bdf5308f03f1081bbfc52132302d

Contents?: true

Size: 1.62 KB

Versions: 8

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true
require "pathname"

module ThemeCheck
  class Theme
    DEFAULT_LOCALE_REGEXP = %r{^locales/(.*)\.default$}
    LIQUID_REGEX = /\.liquid$/i
    JSON_REGEX = /\.json$/i

    def initialize(storage)
      @storage = storage
    end

    def assets
      @assets ||= @storage.files
        .select { |path| path.start_with?("assets/") }
        .map { |path| AssetFile.new(path, @storage) }
    end

    def liquid
      @liquid ||= @storage.files
        .select { |path| LIQUID_REGEX.match?(path) }
        .map { |path| Template.new(path, @storage) }
    end

    def json
      @json ||= @storage.files
        .select { |path| JSON_REGEX.match?(path) }
        .map { |path| JsonFile.new(path, @storage) }
    end

    def directories
      @storage.directories
    end

    def default_locale_json
      return @default_locale_json if defined?(@default_locale_json)
      @default_locale_json = json.find do |json_file|
        json_file.name.match?(DEFAULT_LOCALE_REGEXP)
      end
    end

    def default_locale
      if default_locale_json
        default_locale_json.name[DEFAULT_LOCALE_REGEXP, 1]
      else
        "en"
      end
    end

    def all
      @all ||= json + liquid + assets
    end

    def [](name_or_relative_path)
      case name_or_relative_path
      when Pathname
        all.find { |t| t.relative_path == name_or_relative_path }
      else
        all.find { |t| t.name == name_or_relative_path }
      end
    end

    def templates
      liquid.select(&:template?)
    end

    def sections
      liquid.select(&:section?)
    end

    def snippets
      liquid.select(&:snippet?)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
theme-check-1.2.0 lib/theme_check/theme.rb
theme-check-1.1.0 lib/theme_check/theme.rb
theme-check-1.0.0 lib/theme_check/theme.rb
theme-check-0.10.2 lib/theme_check/theme.rb
theme-check-0.10.1 lib/theme_check/theme.rb
theme-check-0.10.0 lib/theme_check/theme.rb
theme-check-0.9.1 lib/theme_check/theme.rb
theme-check-0.9.0 lib/theme_check/theme.rb