Sha256: b04f0980b92376258836e1f59cb7f5ef7dbf1b566c85ebeb7abcc7020da984ab

Contents?: true

Size: 1.6 KB

Versions: 10

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require "json"

require "spout/models/record"
require "spout/models/option"

module Spout
  module Models
    class Domain < Spout::Models::Record
      attr_accessor :id, :folder, :options
      attr_reader :errors

      def initialize(file_name, dictionary_root)
        @errors = []
        @id = file_name.to_s.gsub(/^(.*)\/|\.json$/, "").downcase

        @folder = file_name.to_s.gsub(/^#{dictionary_root}\/domains\/|#{@id}\.json$/, "")
        @options = []

        json = begin
          if File.exist?(file_name)
            JSON.parse(File.read(file_name))
          else
            @errors << "No corresponding #{@id}.json file found."
            nil
          end
        rescue => e
          @errors << "Parsing error found in #{@id}.json: #{e.message}" unless file_name.nil?
          nil
        end

        if json.is_a? Array
          @id = file_name.to_s.gsub(/^(.*)\/|\.json$/, "").downcase
          @options = (json || []).collect do |option|
            Spout::Models::Option.new(option)
          end
        elsif json
          @errors << "Domain must be a valid array in the following format: [\n  {\n    \"value\": \"1\",\n    \"display_name\": \"First Choice\",\n    \"description\": \"First Description\"\n  },\n  {\n    \"value\": \"2\",\n    \"display_name\": \"Second Choice\",\n    \"description\": \"Second Description\"\n  }\n]"
        end
      end

      def deploy_params
        { name: id, folder: folder.to_s.gsub(%r{/$}, ""),
          options: options.collect(&:deploy_params),
          spout_version: Spout::VERSION::STRING }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spout-1.0.0.beta1 lib/spout/models/domain.rb
spout-0.14.1 lib/spout/models/domain.rb
spout-0.14.0 lib/spout/models/domain.rb
spout-0.14.0.rc lib/spout/models/domain.rb
spout-0.14.0.beta3 lib/spout/models/domain.rb
spout-0.14.0.beta2 lib/spout/models/domain.rb
spout-0.14.0.beta1 lib/spout/models/domain.rb
spout-0.13.0 lib/spout/models/domain.rb
spout-0.13.0.beta2 lib/spout/models/domain.rb
spout-0.13.0.beta1 lib/spout/models/domain.rb