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 initialize all date attributes: :month , :day_of_month , :year

parameters:

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



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 26

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:



17
18
19
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 17

def day_of_month
  @day_of_month
end

#monthObject

:nodoc:



17
18
19
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 17

def month
  @month
end

#yearObject

:nodoc:



17
18
19
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 17

def year
  @year
end

Instance Method Details

#defaultsObject

Assigns default values if values do not exist.



62
63
64
65
66
67
68
69
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 62

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/urbanopt/reporting/default_reports/date.rb', line 45

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