Class: URBANopt::Reporting::DefaultReports::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reporting/default_reports/date.rb

Overview

Date class include information of simulation run date.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Date

Date class intialize all date attributes: :month , :day_of_month , :year

parameters:

hash - Hash - A hash which may contain a deserialized date.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 50

def initialize(hash = {})
  hash.delete_if { |k, v| v.nil? }
  hash = defaults.merge(hash)

  @month = hash[:month].to_i
  @day_of_month = hash[:day_of_month].to_i
  @year = hash[:year].to_i

  # initialize class variables @@validator and @@schema
  @@validator ||= Validator.new
  @@schema ||= @@validator.schema
end

Instance Attribute Details

#day_of_monthObject

:nodoc:



42
43
44
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 42

def day_of_month
  @day_of_month
end

#monthObject

:nodoc:



42
43
44
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 42

def month
  @month
end

#yearObject

:nodoc:



42
43
44
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 42

def year
  @year
end

Instance Method Details

#defaultsObject

Assigns default values if values do not exist.



86
87
88
89
90
91
92
93
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 86

def defaults
  hash = {}
  hash[:month] = nil
  hash[:day_of_month] = nil
  hash[:year] = nil

  return hash
end

#to_hashObject

Converts to a hash equivalent for JSON serialization.

  • Exclude attributes with nil values.

  • Validate date properties against schema.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 69

def to_hash
  result = {}
  result[:month] = @month if @month
  result[:day_of_month] = @day_of_month if @day_of_month
  result[:year] = @year if @year

  # validate date hash properties against schema
  if @@validator.validate(@@schema[:definitions][:Date][:properties], result).any?
    raise "end_uses properties does not match schema: #{@@validator.validate(@@schema[:definitions][:Date][:properties], result)}"
  end

  return result
end