Class: AocCli::Database::Calendar::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/aoc_cli/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(u: Metafile.get(:user), y: Metafile.get(:year), stars:) ⇒ Init

Returns a new instance of Init.



168
169
170
171
172
173
174
175
176
177
# File 'lib/aoc_cli/database.rb', line 168

def initialize(u:Metafile.get(:user),
			   y:Metafile.get(:year), 
			   stars:)
	@user = Validate.user(u)
	@year = Validate.year(y)
	@stars = stars
	@db   = Query
		.new(path:Paths::Database.cfg(user))
		.table(t:"calendar", cols:cols)
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



167
168
169
# File 'lib/aoc_cli/database.rb', line 167

def db
  @db
end

#starsObject (readonly)

Returns the value of attribute stars.



167
168
169
# File 'lib/aoc_cli/database.rb', line 167

def stars
  @stars
end

#userObject (readonly)

Returns the value of attribute user.



167
168
169
# File 'lib/aoc_cli/database.rb', line 167

def user
  @user
end

#yearObject (readonly)

Returns the value of attribute year.



167
168
169
# File 'lib/aoc_cli/database.rb', line 167

def year
  @year
end

Instance Method Details

#colsObject



178
179
180
181
182
# File 'lib/aoc_cli/database.rb', line 178

def cols
	{ year: :INT,
	   day: :INT,
	 stars: :TEXT }
end

#day_data(day) ⇒ Object



186
187
188
# File 'lib/aoc_cli/database.rb', line 186

def day_data(day)
	["'#{year}'", "'#{day}'", "'#{n_stars(day)}'"]
end

#insertObject



192
193
194
195
196
197
198
# File 'lib/aoc_cli/database.rb', line 192

def insert
	unless table_exist?
		1.upto(25){|day| 
			db.insert(t:"calendar", 
					  val:day_data(day))}
	end
end

#n_stars(day) ⇒ Object



183
184
185
# File 'lib/aoc_cli/database.rb', line 183

def n_stars(day)
	stars.keys.include?(day) ? stars[day] : 0
end

#table_exist?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/aoc_cli/database.rb', line 189

def table_exist?
	db.select(t:"calendar", where:{year:"'#{year}'"}).count > 0
end