Class: Anthro::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/anthro.rb

Constant Summary collapse

VALID_MEASUREMENTS =
DATA.keys.freeze
VALID_SEXES =
%w[male female m f].freeze
AGE_RANGE_MONTHS =
(0..240)
DAYS_PER_MONTH =
30.4375

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(measurement_type:, sex:, value:, age_months: nil, age_days: nil) ⇒ Calculator

Returns a new instance of Calculator.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/anthro.rb', line 46

def initialize(measurement_type:, sex:, value:, age_months: nil, age_days: nil)
  @measurement_type = measurement_type.to_sym
  @sex = sex[0].downcase
  @value = value.to_f
  @reference_data = Anthro.reference_data # Use module-level cached data

  raise ArgumentError, "Specify either age_months or age_days, not both" if age_months && age_days
  raise ArgumentError, "Either age_months or age_days must be provided" unless age_months || age_days

  @key_value = if age_months
                 age_months.to_f
               else
                 age_days.to_f / DAYS_PER_MONTH
               end

  validate_inputs
  @z_score = calculate_z_score
  @percentile = calculate_percentile
end

Instance Attribute Details

#percentileObject (readonly)

Returns the value of attribute percentile.



44
45
46
# File 'lib/anthro.rb', line 44

def percentile
  @percentile
end

#z_scoreObject (readonly)

Returns the value of attribute z_score.



44
45
46
# File 'lib/anthro.rb', line 44

def z_score
  @z_score
end