Module: LUSI::API::Core::Util

Included in:
LUSI::API::Calendar::Year
Defined in:
lib/lusi_api/core/util.rb

Instance Method Summary collapse

Instance Method Details

#lusi_year(year_identity = nil, lookup = nil, as_instance: false, offset: 0) ⇒ Integer

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

Parameters:

  • year_identity (String) (defaults to: nil)

    the year identity code

  • lookup (LUSI::API::Lookup::LookupService) (defaults to: nil)

    the LUSI lookup service

  • as_instance (Boolean) (defaults to: false)

    if true, return a LUSI::API::Calendar::Year instance instead of a year

  • offset (Integer) (defaults to: 0)

    an offset (in years) to apply to the year

Returns:

  • (Integer)

    the year represented by the year identity code



18
19
20
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
53
54
55
56
57
58
# 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) ⇒ String

Converts a year into a LUSI year identity code

Parameters:

Returns:

  • (String)

    the year identity code



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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