mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 17:21:38 +00:00
Move update_quality into src/gilded_rose.gleam
This commit is contained in:
parent
c9f4526554
commit
9f9971ebac
@ -1,11 +1,12 @@
|
||||
import argv
|
||||
import gilded_rose_item.{type GildedRose}
|
||||
import gleam/function
|
||||
import gleam/io
|
||||
import glint
|
||||
import glint/flag
|
||||
import test_fixture.{simulate}
|
||||
|
||||
pub fn run_cli_app() {
|
||||
pub fn run_cli_app(modify_inventory: fn(GildedRose) -> GildedRose) {
|
||||
let days_flag = "days"
|
||||
|
||||
let number_of_days =
|
||||
@ -18,7 +19,7 @@ pub fn run_cli_app() {
|
||||
|
||||
let assert Ok(number_of_days) =
|
||||
flag.get_int(from: input.flags, for: days_flag)
|
||||
simulate(number_of_days)
|
||||
simulate(number_of_days, modify_inventory)
|
||||
}
|
||||
|
||||
let app =
|
||||
|
||||
@ -1,5 +1,108 @@
|
||||
import cli
|
||||
import gilded_rose_item.{type GildedRose, type Item, Item}
|
||||
import gleam/list
|
||||
|
||||
pub fn update_quality(inventory: GildedRose) -> GildedRose {
|
||||
let update_quality_item = fn(item: Item) {
|
||||
let new_quality = case
|
||||
item.name != "Aged Brie"
|
||||
&& item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
{
|
||||
True -> {
|
||||
case item.quality > 0 {
|
||||
True ->
|
||||
case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True -> item.quality - 1
|
||||
False -> item.quality
|
||||
}
|
||||
False -> item.quality
|
||||
}
|
||||
}
|
||||
False -> {
|
||||
case item.quality < 50 {
|
||||
True ->
|
||||
item.quality
|
||||
+ 1
|
||||
+ case item.name == "Backstage passes to a TAFKAL80ETC concert" {
|
||||
True ->
|
||||
case item.sell_in < 11 {
|
||||
True -> {
|
||||
case item.quality < 49 {
|
||||
True ->
|
||||
1
|
||||
+ case item.sell_in < 6 {
|
||||
True ->
|
||||
case item.quality < 48 {
|
||||
True -> 1
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> item.quality
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let new_sell_in = case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True -> item.sell_in - 1
|
||||
False -> item.sell_in
|
||||
}
|
||||
|
||||
case new_sell_in < 0 {
|
||||
True ->
|
||||
case item.name != "Aged Brie" {
|
||||
True ->
|
||||
case item.name != "Backstage passes to a TAFKAL80ETC concert" {
|
||||
True ->
|
||||
case new_quality > 0 {
|
||||
True ->
|
||||
case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True ->
|
||||
Item(
|
||||
..item,
|
||||
sell_in: new_sell_in,
|
||||
quality: new_quality - 1,
|
||||
)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: 80)
|
||||
}
|
||||
False ->
|
||||
Item(..item, sell_in: new_sell_in, quality: new_quality)
|
||||
}
|
||||
False ->
|
||||
Item(
|
||||
..item,
|
||||
sell_in: new_sell_in,
|
||||
quality: new_quality - new_quality,
|
||||
)
|
||||
}
|
||||
False ->
|
||||
case new_quality < 50 {
|
||||
True ->
|
||||
Item(..item, sell_in: new_sell_in, quality: new_quality + 1)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: 50)
|
||||
}
|
||||
}
|
||||
False ->
|
||||
case item.name == "Sulfuras, Hand of Ragnaros" {
|
||||
True -> Item(..item, sell_in: new_sell_in, quality: 80)
|
||||
False ->
|
||||
case new_quality > 50 {
|
||||
True -> Item(..item, sell_in: new_sell_in, quality: 50)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: new_quality)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
list.map(inventory, update_quality_item)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
cli.run_cli_app()
|
||||
cli.run_cli_app(update_quality)
|
||||
}
|
||||
|
||||
@ -1,109 +1,6 @@
|
||||
import gleam/list
|
||||
|
||||
pub type GildedRose =
|
||||
List(Item)
|
||||
|
||||
pub type Item {
|
||||
Item(name: String, sell_in: Int, quality: Int)
|
||||
}
|
||||
|
||||
pub fn update_quality(inventory: GildedRose) -> GildedRose {
|
||||
let update_quality_item = fn(item: Item) {
|
||||
let new_quality = case
|
||||
item.name != "Aged Brie"
|
||||
&& item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
{
|
||||
True -> {
|
||||
case item.quality > 0 {
|
||||
True ->
|
||||
case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True -> item.quality - 1
|
||||
False -> item.quality
|
||||
}
|
||||
False -> item.quality
|
||||
}
|
||||
}
|
||||
False -> {
|
||||
case item.quality < 50 {
|
||||
True ->
|
||||
item.quality
|
||||
+ 1
|
||||
+ case item.name == "Backstage passes to a TAFKAL80ETC concert" {
|
||||
True ->
|
||||
case item.sell_in < 11 {
|
||||
True -> {
|
||||
case item.quality < 49 {
|
||||
True ->
|
||||
1
|
||||
+ case item.sell_in < 6 {
|
||||
True ->
|
||||
case item.quality < 48 {
|
||||
True -> 1
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> 0
|
||||
}
|
||||
False -> item.quality
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let new_sell_in = case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True -> item.sell_in - 1
|
||||
False -> item.sell_in
|
||||
}
|
||||
|
||||
case new_sell_in < 0 {
|
||||
True ->
|
||||
case item.name != "Aged Brie" {
|
||||
True ->
|
||||
case item.name != "Backstage passes to a TAFKAL80ETC concert" {
|
||||
True ->
|
||||
case new_quality > 0 {
|
||||
True ->
|
||||
case item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
True ->
|
||||
Item(
|
||||
..item,
|
||||
sell_in: new_sell_in,
|
||||
quality: new_quality - 1,
|
||||
)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: 80)
|
||||
}
|
||||
False ->
|
||||
Item(..item, sell_in: new_sell_in, quality: new_quality)
|
||||
}
|
||||
False ->
|
||||
Item(
|
||||
..item,
|
||||
sell_in: new_sell_in,
|
||||
quality: new_quality - new_quality,
|
||||
)
|
||||
}
|
||||
False ->
|
||||
case new_quality < 50 {
|
||||
True ->
|
||||
Item(..item, sell_in: new_sell_in, quality: new_quality + 1)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: 50)
|
||||
}
|
||||
}
|
||||
False ->
|
||||
case item.name == "Sulfuras, Hand of Ragnaros" {
|
||||
True -> Item(..item, sell_in: new_sell_in, quality: 80)
|
||||
False ->
|
||||
case new_quality > 50 {
|
||||
True -> Item(..item, sell_in: new_sell_in, quality: 50)
|
||||
False -> Item(..item, sell_in: new_sell_in, quality: new_quality)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
list.map(inventory, update_quality_item)
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import gilded_rose_item.{type GildedRose, type Item, Item, update_quality}
|
||||
import gilded_rose_item.{type GildedRose, type Item, Item}
|
||||
import gleam/int
|
||||
import gleam/io
|
||||
import gleam/list
|
||||
@ -15,7 +15,10 @@ const test_fixture = [
|
||||
Item("Conjured Mana Cake", 3, 6),
|
||||
]
|
||||
|
||||
pub fn simulate(number_of_days: Int) -> GildedRose {
|
||||
pub fn simulate(
|
||||
number_of_days: Int,
|
||||
modify_inventory: fn(GildedRose) -> GildedRose,
|
||||
) -> GildedRose {
|
||||
let days = list.range(0, number_of_days)
|
||||
let display_item = fn(item: Item) {
|
||||
string.join(
|
||||
@ -34,6 +37,6 @@ pub fn simulate(number_of_days: Int) -> GildedRose {
|
||||
))
|
||||
io.println("name, sellIn, quality")
|
||||
list.each(inventory, fn(item) { io.println(display_item(item)) })
|
||||
update_quality(inventory)
|
||||
modify_inventory(inventory)
|
||||
})
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import gilded_rose_item.{Item, update_quality}
|
||||
import gilded_rose.{update_quality}
|
||||
import gilded_rose_item.{Item}
|
||||
import gleeunit
|
||||
import gleeunit/should
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user