Class: Spout::Models::Variable
- Defined in:
- lib/spout/models/variable.rb
Instance Attribute Summary collapse
-
#calculation ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#commonly_used ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#description ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#display_name ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#domain_name ⇒ Object
Returns the value of attribute domain_name.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#folder ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#form_names ⇒ Object
Returns the value of attribute form_names.
-
#forms ⇒ Object
Returns the value of attribute forms.
-
#id ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#labels ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#max ⇒ Object
Returns the value of attribute max.
-
#mean ⇒ Object
Returns the value of attribute mean.
-
#median ⇒ Object
Returns the value of attribute median.
-
#min ⇒ Object
Returns the value of attribute min.
-
#n ⇒ Object
Returns the value of attribute n.
-
#stddev ⇒ Object
Returns the value of attribute stddev.
-
#total ⇒ Object
Returns the value of attribute total.
-
#type ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#units ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”].
-
#unknown ⇒ Object
Returns the value of attribute unknown.
Class Method Summary collapse
Instance Method Summary collapse
- #deploy_params ⇒ Object
-
#initialize(file_name, dictionary_root) ⇒ Variable
constructor
A new instance of Variable.
- #known_issues ⇒ Object
- #path ⇒ Object
Methods inherited from Record
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. 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
#calculation ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def calculation @calculation end |
#commonly_used ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def commonly_used @commonly_used end |
#description ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def description @description end |
#display_name ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def display_name @display_name end |
#domain ⇒ Object
Returns the value of attribute domain.
17 18 19 |
# File 'lib/spout/models/variable.rb', line 17 def domain @domain end |
#domain_name ⇒ Object
Returns the value of attribute domain_name.
16 17 18 |
# File 'lib/spout/models/variable.rb', line 16 def domain_name @domain_name end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
19 20 21 |
# File 'lib/spout/models/variable.rb', line 19 def errors @errors end |
#folder ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def folder @folder end |
#form_names ⇒ Object
Returns the value of attribute form_names.
16 17 18 |
# File 'lib/spout/models/variable.rb', line 16 def form_names @form_names end |
#forms ⇒ Object
Returns the value of attribute forms.
17 18 19 |
# File 'lib/spout/models/variable.rb', line 17 def forms @forms end |
#id ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def id @id end |
#labels ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def labels @labels end |
#max ⇒ Object
Returns the value of attribute max.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def max @max end |
#mean ⇒ Object
Returns the value of attribute mean.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def mean @mean end |
#median ⇒ Object
Returns the value of attribute median.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def median @median end |
#min ⇒ Object
Returns the value of attribute min.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def min @min end |
#n ⇒ Object
Returns the value of attribute n.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def n @n end |
#stddev ⇒ Object
Returns the value of attribute stddev.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def stddev @stddev end |
#total ⇒ Object
Returns the value of attribute total.
18 19 20 |
# File 'lib/spout/models/variable.rb', line 18 def total @total end |
#type ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def type @type end |
#units ⇒ Object
VARIABLE_TYPES = [“choices”, “numeric”, “integer”]
15 16 17 |
# File 'lib/spout/models/variable.rb', line 15 def units @units end |
#unknown ⇒ Object
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
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
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_params ⇒ Object
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_issues ⇒ Object
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 |
#path ⇒ Object
54 55 56 |
# File 'lib/spout/models/variable.rb', line 54 def path File.join(@folder, "#{@id}.json") end |