Class: Spout::Models::Variable

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

find_by_id

Constructor Details

#initialize(file_name, dictionary_root) ⇒ Variable

Returns a new instance of Variable.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spout/models/variable.rb', line 21

def initialize(file_name, dictionary_root)
  @errors = []
  @id     = file_name.to_s.gsub(%r{^(.*)/|\.json$}, "").downcase
  @folder = file_name.to_s.gsub(%r{^#{dictionary_root}/variables/|#{@id}\.json$}, "")
  @form_names = []
  @domain_name = nil

  json = begin
           JSON.parse(File.read(file_name, encoding: "utf-8"))
         rescue => e
           error = e.message
           nil
         end

  if json.is_a?(Hash)
    %w(display_name description type units commonly_used calculation).each do |method|
      instance_variable_set("@#{method}", json[method])
    end
    @commonly_used = false if @commonly_used.nil?
    @errors << "'id': #{json['id'].inspect} does not match filename #{@id.inspect}" if @id != json["id"]
    @domain_name  = json["domain"] # Spout::Models::Domain.new(json["domain"], dictionary_root)
    @labels       = (json["labels"] || [])
    @form_names   = (json["forms"] || []).collect do |form_name|
      form_name
    end
  elsif json
    @errors << "Variable must be a valid hash in the following format: {\n\"id\": \"VARIABLE_ID\",\n  \"display_name\": \"VARIABLE DISPLAY NAME\",\n  \"description\": \"VARIABLE DESCRIPTION\"\n}"
  end
  @errors = (@errors + [error]).compact
  @domain = Spout::Models::Domain.find_by_id(@domain_name)
  @forms = @form_names.collect { |form_name| Spout::Models::Form.find_by_id(form_name) }.compact
end

Instance Attribute Details

#calculationObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def calculation
  @calculation
end

#commonly_usedObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def commonly_used
  @commonly_used
end

#descriptionObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def description
  @description
end

#display_nameObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def display_name
  @display_name
end

#domainObject

Returns the value of attribute domain.



17
18
19
# File 'lib/spout/models/variable.rb', line 17

def domain
  @domain
end

#domain_nameObject

Returns the value of attribute domain_name.



16
17
18
# File 'lib/spout/models/variable.rb', line 16

def domain_name
  @domain_name
end

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/spout/models/variable.rb', line 19

def errors
  @errors
end

#folderObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def folder
  @folder
end

#form_namesObject

Returns the value of attribute form_names.



16
17
18
# File 'lib/spout/models/variable.rb', line 16

def form_names
  @form_names
end

#formsObject

Returns the value of attribute forms.



17
18
19
# File 'lib/spout/models/variable.rb', line 17

def forms
  @forms
end

#idObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def id
  @id
end

#labelsObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def labels
  @labels
end

#maxObject

Returns the value of attribute max.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def max
  @max
end

#meanObject

Returns the value of attribute mean.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def mean
  @mean
end

#medianObject

Returns the value of attribute median.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def median
  @median
end

#minObject

Returns the value of attribute min.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def min
  @min
end

#nObject

Returns the value of attribute n.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def n
  @n
end

#stddevObject

Returns the value of attribute stddev.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def stddev
  @stddev
end

#totalObject

Returns the value of attribute total.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def total
  @total
end

#typeObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def type
  @type
end

#unitsObject

VARIABLE_TYPES = [“choices”, “numeric”, “integer”]



15
16
17
# File 'lib/spout/models/variable.rb', line 15

def units
  @units
end

#unknownObject

Returns the value of attribute unknown.



18
19
20
# File 'lib/spout/models/variable.rb', line 18

def unknown
  @unknown
end

Class Method Details

.partial_match?(string, term) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/spout/models/variable.rb', line 81

def self.partial_match?(string, term)
  !(/#{term.to_s.downcase}/ =~ string.to_s.downcase).nil?
end

.starts_with?(string, term) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/spout/models/variable.rb', line 77

def self.starts_with?(string, term)
  !(/^#{term.to_s.downcase}/ =~ string.to_s.downcase).nil?
end

Instance Method Details

#deploy_paramsObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/spout/models/variable.rb', line 85

def deploy_params
  { name: id, display_name: display_name, variable_type: type,
    folder: folder.to_s.gsub(%r{/$}, ""), description: description,
    units: units, calculation: calculation, commonly_used: commonly_used,
    labels: labels,
    stats_n: n, stats_mean: mean, stats_stddev: stddev,
    stats_median: median, stats_min: min, stats_max: max,
    stats_unknown: unknown, stats_total: total,
    known_issues: known_issues,
    spout_version: Spout::VERSION::STRING
  }
end

#known_issuesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/spout/models/variable.rb', line 58

def known_issues
  line_found = false
  lines = []
  known_issues_file = "KNOWNISSUES.md"
  if File.exist?(known_issues_file) && File.file?(known_issues_file)
    IO.foreach(known_issues_file) do |line|
      if line_found && Variable.starts_with?(line, "  - ")
        lines << line
      elsif Variable.partial_match?(line, "\\[#{id}\\]")
        line_found = true
        lines << line
      else
        line_found = false
      end
    end
  end
  lines.join("\n")
end

#pathObject



54
55
56
# File 'lib/spout/models/variable.rb', line 54

def path
  File.join(@folder, "#{@id}.json")
end