Sha256: ed5275d66cb79f03c14ac0d4bb8ce9bb6631f0361aff4f0f68a98fd8fd8850a3

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'spout/models/variable'
require 'spout/models/domain'
require 'spout/models/form'

module Spout
  module Models
    # Creates a structure that contains a dictionaries variables, domains, and
    # forms
    class Dictionary
      attr_accessor :variables, :domains, :forms
      attr_accessor :app_path

      attr_reader :variable_files, :domain_files, :form_files

      def initialize(app_path)
        @app_path = app_path

        @variable_files = json_files('variables')
        @domain_files = json_files('domains')
        @form_files = json_files('forms')

        @variables = []
        @domains = []
        @forms = []
      end

      def load_all!
        load_variables!
        load_domains!
        load_forms!
        self
      end

      def load_variables!
        load_type!('Variable')
      end

      def load_domains!
        load_type!('Domain')
      end

      def load_forms!
        load_type!('Form')
      end

      private

      def json_files(type)
        Dir.glob(File.join(@app_path, type, '**', '*.json'))
      end

      def load_type!(method)
        results = instance_variable_get("@#{method.downcase}_files").collect do |file|
          Object.const_get("Spout::Models::#{method}").new(file, @app_path)
        end

        instance_variable_set("@#{method.downcase}s", results)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spout-0.12.1 lib/spout/models/dictionary.rb
spout-0.12.0 lib/spout/models/dictionary.rb
spout-0.12.0.rc2 lib/spout/models/dictionary.rb
spout-0.12.0.rc lib/spout/models/dictionary.rb
spout-0.12.0.beta2 lib/spout/models/dictionary.rb
spout-0.12.0.beta1 lib/spout/models/dictionary.rb
spout-0.11.1 lib/spout/models/dictionary.rb