Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconfiguring Country class #2

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions source/classes/collections/country.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
import {Config} from './config.mjs'
*/
import {cleanseIsoCode} from '../helpers/conversions.mjs'

// ⚠️ [TODO] REMOVE TEMPORARY DEV CODE
const eject = (instance) => (JSON.parse(JSON.stringify(instance)))
class List extends Array { }


class Country {
/* STEP 1: INITIALIZE CLASS ATTRIBUTE STRUCTURE */
// Static config info (ℹ️ must be initialized by app)
static config = undefined

// Universal country code
'iso3166-1' = undefined

// Country name in english
name = undefined


/* STEP 2: APPLY NEW INSTANCE CONSTRUCTION */
constructor (data = { }) {
let self = this // allow the forgetting of "this"
data = {...data} // dont mutate input data

// If the data already has an instance of this class,
// then there is no point in creating a new instance.
// We can replace "self" instance, thus forgetting it.
if (data.country instanceof Country) {
self = data.country
delete data.country
}

// The "self" variable can either be "this",
// or another class instance obtained from the data.
self.assignData(data)

return self // override the returning of "this"
}


/* STEP 3: CLEAN AND ASSIGN DATA INPUT TO THE INSTANCE */
assignData ({country}) {
//+ CLEAN THE COUNTRY DATA +//
if (country == null) {
return
}
else if (country.ids == null) {
({country} = Country.parseFromAPI({country}))
}
else {
({country} = Country.parseFromDB({country}))
}

//+ ASSIGN THE COUNTRY DATA +//
this['iso3166-1'] = country['iso3166-1']
this.name = country.name
}


toJSON ( ) {
return this
}


static parseFromAPI ({country}) {
const newCountry = eject(new Country())
newCountry['iso3166-1'] = cleanseIsoCode(country.iso_3166_1)
newCountry.name = country.name
return {country: newCountry}
}


static parseFromDB ({country}) {
return {country}
}


// TODO: Add "matches"
static matches () {throw new Error('⚠️ Need to implement matches on Country!')}

// TODO: Add "combine"
static combine () {throw new Error('⚠️ Need to implement combine on Country!')}

// TODO: Add "sharedMetadata"
static #sharedMetadata () {throw new Error('⚠️ Need to implement #sharedMetadata on Country!')}
}

export {Country}
2 changes: 1 addition & 1 deletion source/classes/company.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Config} from './config.mjs'
import {Country} from './country.mjs'
import {Country} from './collections/country.mjs'
import {Logo} from './image.mjs'

class Company {
Expand Down
84 changes: 0 additions & 84 deletions source/classes/country.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion source/classes/movie.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {convertToEasyDuration} from '../helpers/conversions.mjs'
import {Collection} from './collection.mjs'
import {Company} from './company.mjs'
import {Config} from './config.mjs'
import {Country} from './country.mjs'
import {Country} from './collections/country.mjs'
import {Genre} from './collections/genre.mjs'
import {Image, Backdrop, Poster} from './image.mjs'
import {Language} from './language.mjs'
Expand Down
2 changes: 1 addition & 1 deletion source/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Collection} from './classes/collection.mjs'
import {Comment} from './classes/comment.mjs'
import {Company} from './classes/company.mjs'
import {Config} from './classes/config.mjs'
import {Country} from './classes/country.mjs'
import {Country} from './classes/collections/country.mjs'
import {Genre} from './classes/collections/genre.mjs'
import {
Image,
Expand Down