module LUSI::API::Core::Util

Public Instance Methods

lusi_year(year_identity = nil, lookup = nil, as_instance: false, offset: 0) click to toggle source

Converts a LUSI year identity code into a numeric or LUSI::API::Calendar::Year instance representing the year If a lookup service is supplied, the identity code is resolved by a LUSI API call. Otherwise, the year is calculated as: year = year_identity.to_i + 1900 @param year_identity [String] the year identity code @param lookup [LUSI::API::Lookup::LookupService] the LUSI lookup service @param as_instance [Boolean] if true, return a LUSI::API::Calendar::Year instance instead of a year @param offset [Integer] an offset (in years) to apply to the year @return [Integer] the year represented by the year identity code

# File lib/lusi_api/core/util.rb, line 18
def lusi_year(year_identity = nil, lookup = nil, as_instance: false, offset: 0)

  # Convert the year identity to an integer
  case
    when year_identity.is_a?(LUSI::API::Calendar::Week)
      year_identity = year_identity.year.identity.to_i
    when year_identity.is_a?(LUSI::API::Calendar::Year)
      year_identity = year_identity.identity.to_i
    when year_identity.is_a?(String) && year_identity.length == 6
      # Assume a 6-character string is an identity code
      year_identity = year_identity.to_i
    else
      # Assume anything else is a year and convert it to an identity
      year_identity = lusi_year_identity(year.to_i)
  end

  # Apply the offset
  year_identity += offset.to_i

  year_identity = "%06d" % year_identity
  if lookup
    year = lookup.lookup(:year, year_identity)
    if as_instance
      year
    else
      year ? year.full_year.to_i : nil
    end
  else
    # Year identities are (year - 1900) zero-padded to six digits
    year = year_identity.to_i + 1900
    if as_instance
      yy = year % 100
      description = "%02s/%02s" % [yy, (yy + 1) % 100]
      LUSI::API::Calendar::Year.new(nil, lookup, code: year_identity, description: description,
                                    full_year: year.to_s)
    else
      year
    end
  end

end
lusi_year_identity(year = nil, offset: 0) click to toggle source

Converts a year into a LUSI year identity code @param year [Date, DateTime, Numeric, String, LUSI::API::Calendar::Week, LUSI::API::Calendar::Year] the year @param offset [Integer] an offset (in years) to apply to the year @return [String] the year identity code

# File lib/lusi_api/core/util.rb, line 64
def lusi_year_identity(year = nil, offset: 0)

  offset = offset.to_i

  # Get the year as an integer
  # If the year is provided as an identity, it's converted to a numeric year
  year_identity = nil
  case
    when year.is_a?(LUSI::API::Calendar::Week)
      year_identity = year.year.identity
    when year.is_a?(LUSI::API::Calendar::Year)
      year_identity = year.identity
    when year.is_a?(Date) || year.is_a?(DateTime) || year.is_a?(Time)
      year = year.year
    when year.is_a?(String)
      if year.length == 6
        year_identity = year
      else
        year = year.to_i
      end
    when year.is_a?(Numeric)
      year = year.to_i
    else
      year = Time.now.year
  end
  year = year_identity.to_i + 1900 if year_identity

  # Return the year identity for the year with offset applied
  "%06d" % (year + offset - 1900)

end