nepdate-patro 1.0.0.rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NepDate
4
+ module Patro
5
+ VERSION = "1.0.0.rc.1"
6
+ end
7
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "nepdate"
4
+ require_relative "patro/version"
5
+ require_relative "patro/table"
6
+ require_relative "patro/named"
7
+ require_relative "patro/calendar_info"
8
+
9
+ module NepDate
10
+ # The patro (पात्रो, the Nepali almanac): tithi, public holidays and events per BS day. It is
11
+ # a separate gem that depends on +nepdate+; the core gem has no almanac data.
12
+ module Patro
13
+ # Builds one immutable CalendarInfo per coverage day, sharing the tithi and event objects. A
14
+ # lookup is then an array read, as in the Python port, instead of a binary search and three
15
+ # allocations per call.
16
+ def self.build_day_info(serial, holiday, event_days)
17
+ tithi = if serial.between?(Table::TITHI_FIRST_SERIAL, Table::TITHI_LAST_SERIAL)
18
+ TITHI_OBJECTS[Table::TITHI_INDEX[serial - Table::TITHI_FIRST_SERIAL]]
19
+ end
20
+ public_holiday = holiday.key?(serial)
21
+ events = EMPTY_EVENTS
22
+ position = event_days[serial]
23
+ if position
24
+ first = Table::EVENT_START[position]
25
+ last = Table::EVENT_START[position + 1]
26
+ events = Array.new(last - first) { |offset| EVENT_OBJECTS[Table::EVENTS_FLAT[first + offset]] }
27
+ end
28
+
29
+ if tithi.nil? && !public_holiday && events.empty?
30
+ EMPTY_INFO
31
+ else
32
+ CalendarInfo.new(tithi: tithi, public_holiday: public_holiday, events: events)
33
+ end
34
+ end
35
+ private_class_method :build_day_info
36
+
37
+ TITHI_OBJECTS = Table::NAMES.map { |nepali, english| Tithi.new(nepali, english) }.freeze
38
+ EVENT_OBJECTS = Table::NAMES.map { |nepali, english| Event.new(nepali, english) }.freeze
39
+ EMPTY_EVENTS = [].freeze
40
+ EMPTY_INFO = CalendarInfo.new
41
+ DAY_INFO = begin
42
+ holiday = {}
43
+ Table::HOLIDAY_SERIALS.each { |serial| holiday[serial] = true }
44
+ event_days = {}
45
+ Table::EVENT_DAY_SERIALS.each_with_index { |serial, index| event_days[serial] = index }
46
+ info = Array.new(CalendarTable::TOTAL_DAYS, EMPTY_INFO)
47
+ (Table::FIRST_SERIAL..Table::LAST_SERIAL).each do |serial|
48
+ info[serial] = build_day_info(serial, holiday, event_days)
49
+ end
50
+ info.freeze
51
+ end
52
+ COVERAGE = NepDate::DateRange.new(
53
+ NepDate::Date.from_serial(Table::FIRST_SERIAL),
54
+ NepDate::Date.from_serial(Table::LAST_SERIAL)
55
+ )
56
+
57
+ class << self
58
+ # Everything the patro knows about +date+, never nil for the date itself. A day without
59
+ # data returns a nil tithi, a false holiday flag and no events.
60
+ def info(date)
61
+ raise TypeError, "expected a NepDate::Date, got #{date.class}" unless date.is_a?(NepDate::Date)
62
+
63
+ DAY_INFO[date.serial]
64
+ end
65
+ alias_method :of, :info
66
+
67
+ # The smallest BS range containing every day with tithi, holiday or event data.
68
+ def coverage
69
+ COVERAGE
70
+ end
71
+
72
+ # Every gazetted public holiday, ascending.
73
+ def public_holidays
74
+ Table::HOLIDAY_SERIALS.map { |serial| NepDate::Date.from_serial(serial) }
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ module NepDate
81
+ class Date
82
+ # One lookup returning the tithi, holiday flag and events of this date. It comes from the
83
+ # patro gem, so the core has no reference to it (D-13).
84
+ def calendar_info
85
+ Patro.info(self)
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,58 @@
1
+ module NepDate
2
+ module Patro
3
+ VERSION: String
4
+
5
+ module Lookup
6
+ def self.index_of: (Array[Integer], Integer) -> Integer?
7
+ def self.include?: (Array[Integer], Integer) -> bool
8
+ end
9
+
10
+ class Named
11
+ attr_reader nepali: String
12
+ attr_reader english: String
13
+
14
+ def initialize: (String, String) -> void
15
+ def name: (?lang: Symbol | String) -> String
16
+ def to_h: () -> Hash[Symbol, String]
17
+ def ==: (untyped) -> bool
18
+ def eql?: (untyped) -> bool
19
+ def hash: () -> Integer
20
+ def to_s: () -> String
21
+ def inspect: () -> String
22
+ end
23
+
24
+ class Tithi < Named
25
+ end
26
+
27
+ class Event < Named
28
+ end
29
+
30
+ class CalendarInfo
31
+ attr_reader tithi: Tithi?
32
+ attr_reader events: Array[Event]
33
+
34
+ def initialize: (?tithi: Tithi?, ?public_holiday: bool, ?events: Array[Event]) -> void
35
+ def public_holiday?: () -> bool
36
+ def is_public_holiday: () -> bool
37
+ def holiday?: () -> bool
38
+ def tithi_np: () -> String
39
+ def tithi_en: () -> String
40
+ def events_np: () -> Array[String]
41
+ def events_en: () -> Array[String]
42
+ def to_h: () -> Hash[Symbol, untyped]
43
+ def ==: (untyped) -> bool
44
+ def eql?: (untyped) -> bool
45
+ def hash: () -> Integer
46
+ def inspect: () -> String
47
+ end
48
+
49
+ def self.info: (NepDate::Date) -> CalendarInfo
50
+ def self.of: (NepDate::Date) -> CalendarInfo
51
+ def self.coverage: () -> NepDate::DateRange
52
+ def self.public_holidays: () -> Array[NepDate::Date]
53
+ end
54
+
55
+ class Date
56
+ def calendar_info: () -> NepDate::Patro::CalendarInfo
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nepdate-patro
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc.1
5
+ platform: ruby
6
+ authors:
7
+ - Sanam Pakuwal
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: nepdate
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '='
17
+ - !ruby/object:Gem::Version
18
+ version: 1.0.0.rc.1
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - '='
24
+ - !ruby/object:Gem::Version
25
+ version: 1.0.0.rc.1
26
+ description: |
27
+ The patro (पात्रो, the Nepali almanac) as a separate gem for NepDate for Ruby. It answers the
28
+ tithi, the gazetted holiday flag and the ordered event list for every covered BS date, with
29
+ the data compiled into the gem. It depends on nepdate and the core has no almanac data.
30
+ email:
31
+ - 31341013+sanamhub@users.noreply.github.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - LICENSE
38
+ - NOTICE
39
+ - README.md
40
+ - lib/nepdate/patro.rb
41
+ - lib/nepdate/patro/calendar_info.rb
42
+ - lib/nepdate/patro/named.rb
43
+ - lib/nepdate/patro/table.rb
44
+ - lib/nepdate/patro/version.rb
45
+ - sig/nepdate-patro.rbs
46
+ homepage: https://nepdate.com
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ bug_tracker_uri: https://github.com/sanamhub/nepdate-ruby/issues
51
+ changelog_uri: https://github.com/sanamhub/nepdate-ruby/blob/main/CHANGELOG.md
52
+ documentation_uri: https://github.com/sanamhub/nepdate-ruby/blob/main/docs/API.md
53
+ homepage_uri: https://nepdate.com
54
+ rubygems_mfa_required: 'true'
55
+ source_code_uri: https://github.com/sanamhub/nepdate-ruby
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '3.4'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.6.9
71
+ specification_version: 4
72
+ summary: 'The Nepali patro for Ruby: tithi, public holidays and events for Bikram
73
+ Sambat dates'
74
+ test_files: []