Skip to content

Commit

Permalink
Add access to calendar data to the MPXJ gem (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
joniles authored Nov 8, 2024
1 parent 1f4cc03 commit e64d959
Show file tree
Hide file tree
Showing 22 changed files with 712 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src.ruby/mpxj/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src.ruby/mpxj/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src.ruby/mpxj/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions src.ruby/mpxj/.idea/mpxj.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src.ruby/mpxj/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions src.ruby/mpxj/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ StringLiterals:
HashSyntax:
Enabled: false

VariableInterpolation:
Enabled: false

TrailingComma:
Enabled: false

Expand Down
5 changes: 5 additions & 0 deletions src.ruby/mpxj/lib/mpxj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require "mpxj/project"
require "mpxj/property_methods"
require "mpxj/properties"
require "mpxj/calendar"
require "mpxj/calendar_day"
require "mpxj/calendar_week"
require "mpxj/calendar_hours"
require "mpxj/calendar_exception"
require "mpxj/resource_methods"
require "mpxj/resource"
require "mpxj/task_methods"
Expand Down
112 changes: 112 additions & 0 deletions src.ruby/mpxj/lib/mpxj/calendar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
module MPXJ
# Represents a calendar
class Calendar < Container
attr_reader :days
attr_reader :weeks
attr_reader :exceptions

def initialize(parent_project, attribute_values)
super(parent_project, attribute_values.slice('unique_id', 'guid', 'parent_unique_id', 'name', 'type', 'personal', 'minutes_per_day', 'minutes_per_week', 'minutes_per_month', 'minutes_per_year'))
process_days(attribute_values)
process_weeks(attribute_values)
process_exceptions(attribute_values)
end

# Retrieve the calendar unique ID
#
# @return [Integer] the calendar unique ID
def unique_id
get_integer_value(attribute_values['unique_id'])
end

# Retrieve the calendar GUID
#
# @return [String] the calendar GUID
def guid
attribute_values['guid']
end

# Retrieve the parent calendar unique ID
#
# @return [Integer] the parent calendar unique ID
# @return [nil] if the calendar does not have a parent
def parent_unique_id
get_nillable_integer_value(attribute_values['parent_unique_id'])
end

# Retrieve the parent calendar of this calendar
#
# @return [Calendar] if this calendar is the child of another calendar
# @return [nil] if this is a base calendar
def parent_calendar
parent_project.get_calendar_by_unique_id(attribute_values['parent_unique_id']&.to_i)
end

# Retrieve the calendar name
#
# @return [String] the calendar name
def name
attribute_values['name']
end

# Retrieve the calendar type
#
# @return [String] the calendar type
def type
attribute_values['type']
end

# Retrieve the personal flag
#
# @return [Boolean] true if this is a personal calendar
def personal
get_boolean_value(attribute_values['personal'])
end

# Retrieve the number of minutes per day
#
# @return [Integer] the number of minutes per day
# @return [nil] if this calendar does not provide a value for minutes per day
def minutes_per_day
get_nillable_integer_value(attribute_values['minutes_per_day'])
end

# Retrieve the number of minutes per week
#
# @return [Integer] the number of minutes per week
# @return [nil] if this calendar does not provide a value for minutes per week
def minutes_per_week
get_nillable_integer_value(attribute_values['minutes_per_week'])
end

# Retrieve the number of minutes per month
#
# @return [Integer] the number of minutes per month
# @return [nil] if this calendar does not provide a value for minutes per month
def minutes_per_month
get_nillable_integer_value(attribute_values['minutes_per_month'])
end

# Retrieve the number of minutes per year
#
# @return [Integer] the number of minutes per year
# @return [nil] if this calendar does not provide a value for minutes per year
def minutes_per_year
get_nillable_integer_value(attribute_values['minutes_per_year'])
end

private

def process_days(attribute_values)
@days = attribute_values.slice('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday').map {|name, day| [name, CalendarDay.new(parent_project, day)]}.to_h
end

def process_weeks(attribute_values)
@weeks = (attribute_values['working_weeks'] || []).map {|week| CalendarWeek.new(parent_project, week)}
end

def process_exceptions(attribute_values)
@exceptions = (attribute_values['exceptions'] || []).map {|exception| CalendarException.new(parent_project, exception)}
end
end
end
26 changes: 26 additions & 0 deletions src.ruby/mpxj/lib/mpxj/calendar_day.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module MPXJ
# Represents a calendar day
class CalendarDay < Container
attr_reader :hours

def initialize(parent_project, attribute_values)
super(parent_project, attribute_values.slice('type'))
process_hours(attribute_values)
end

# Retrieve the day type
#
# @return [String] the calendar day type
def type
attribute_values['type']
end

private

def process_hours(attribute_values)
@hours = (attribute_values['hours'] || {}).map do |hours|
CalendarHours.new(parent_project, hours)
end
end
end
end
47 changes: 47 additions & 0 deletions src.ruby/mpxj/lib/mpxj/calendar_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module MPXJ
# Represents a calendar exception
class CalendarException < Container
attr_reader :hours

def initialize(parent_project, attribute_values)
super(parent_project, attribute_values.slice('name', 'from', 'to', 'type'))
process_hours(attribute_values)
end

# Retrieve the exception name
#
# @return [String] the exception name
def name
attribute_values['name']
end

# Retrieve the date on which this exception starts
#
# @return [Time] the exception from date
def from
get_date_value(attribute_values['from'])
end

# Retrieve the date on which this exception ends
#
# @return [Time] the exception to date
def to
get_date_value(attribute_values['to'])
end

# Retrieve the exception type
#
# @return [String] the exception type
def type
attribute_values['type']
end

private

def process_hours(attribute_values)
@hours = (attribute_values['hours'] || {}).map do |hours|
CalendarHours.new(parent_project, hours)
end
end
end
end
19 changes: 19 additions & 0 deletions src.ruby/mpxj/lib/mpxj/calendar_hours.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module MPXJ
# Represents a range of hours
class CalendarHours < Container

# Retrieve the the start hour
#
# @return [Time] start hour
def from
get_date_value(attribute_values['from'])
end

# Retrieve the the finish hour
#
# @return [Time] finish hour
def to
get_date_value(attribute_values['to'])
end
end
end
38 changes: 38 additions & 0 deletions src.ruby/mpxj/lib/mpxj/calendar_week.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module MPXJ
# Represents a working week
class CalendarWeek < Container
attr_reader :days

def initialize(parent_project, attribute_values)
super(parent_project, attribute_values.slice('name', 'effective_from', 'effective_to'))
process_days(attribute_values)
end

# Retrieve the exception name
#
# @return [String] the exception name
def name
attribute_values['name']
end

# Retrieve the date from which this working week is in effect
#
# @return [Time] effective from date
def effective_from
get_date_value(attribute_values['effective_from'])
end

# Retrieve the date to which this working week is in effect
#
# @return [Time] effective to date
def effective_to
get_date_value(attribute_values['effective_to'])
end

private

def process_days(attribute_values)
@days = attribute_values.slice('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday').map {|name, day| [name, CalendarDay.new(parent_project, day)]}.to_h
end
end
end
8 changes: 8 additions & 0 deletions src.ruby/mpxj/lib/mpxj/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def get_integer_value(attribute_value)
end
end

def get_nillable_integer_value(attribute_value)
if attribute_value.nil?
nil
else
attribute_value.to_i
end
end

def get_boolean_value(attribute_value)
attribute_value == true
end
Expand Down
Loading

0 comments on commit e64d959

Please sign in to comment.