Class: Spout::Models::Domain

Inherits:
Record
  • Object
show all
Defined in:
lib/spout/models/domain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

find_by_id

Constructor Details

#initialize(file_name, dictionary_root) ⇒ Domain

Returns a new instance of Domain.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spout/models/domain.rb', line 14

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, encoding: "utf-8"))
    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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/spout/models/domain.rb', line 12

def errors
  @errors
end

#folderObject

Returns the value of attribute folder.



11
12
13
# File 'lib/spout/models/domain.rb', line 11

def folder
  @folder
end

#idObject

Returns the value of attribute id.



11
12
13
# File 'lib/spout/models/domain.rb', line 11

def id
  @id
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/spout/models/domain.rb', line 11

def options
  @options
end

Instance Method Details

#deploy_paramsObject



43
44
45
46
47
# File 'lib/spout/models/domain.rb', line 43

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