From 015010345a804c2e5ae23481e2114236bdb5e17f Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 18 Jun 2024 15:21:43 -0700 Subject: [PATCH] Extract Dyeworks methods into `Item::Dyeworks` module There's just starting to be a lot going on, so I pulled them out into here! I also considered a like, `Item::DyeworksStatus` class, and then you'd go like, `item.dyeworks.buyable?`. But idk, I think it's nice that the current API is simple for callers, and being able to do things like `items.filter(&:dyeworks_buyable?)` is pretty darn convenient. This solution lets us keep the increasing number of Dyeworks methods from polluting the main `item.rb`, while still keeping the API identical! --- app/models/item.rb | 55 +---------------------------------- app/models/item/dyeworks.rb | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 54 deletions(-) create mode 100644 app/models/item/dyeworks.rb diff --git a/app/models/item.rb b/app/models/item.rb index 0e9c174f..83bdbe34 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -3,6 +3,7 @@ require "async/barrier" class Item < ApplicationRecord include PrettyParam + include Item::Dyeworks # We use the `type` column to mean something other than what Rails means! self.inheritance_column = nil @@ -198,60 +199,6 @@ class Item < ApplicationRecord nc_mall_record.present? end - def dyeworks? - dyeworks_base_item.present? - end - - # Whether this is a Dyeworks item whose base item can currently be purchased - # in the NC Mall. It may or may not currently be *dyeable* in the NC Mall, - # because Dyeworks eligibility is often a limited-time event. - def dyeworks_base_buyable? - dyeworks_base_item.present? && dyeworks_base_item.currently_in_mall? - end - - # Whether this is one of the few Dyeworks items that can be dyed in the NC - # Mall at any time, rather than as part of a limited-time event. (Owls tracks - # this, not us!) - def dyeworks_permanent? - return false if nc_trade_value.nil? - nc_trade_value.value_text.include?("Permanent Dyeworks") - end - - # Whether this is a Dyeworks item that can be dyed in the NC Mall ~right now, - # but only as part of a limited-time event. (Owls tracks this, not us!) - def dyeworks_limited? - return false if nc_trade_value.nil? - nc_trade_value.value_text.include?("Limited Dyeworks") - end - - # Whether this is a Dyeworks item that can be dyed in the NC Mall ~right now, - # either at any time or as a limited-time event. (Owls tracks this, not us!) - def dyeworks_dyeable? - dyeworks_permanent? || dyeworks_limited? - end - - # Whether this is a Dyeworks item whose base item can currently be purchased - # in the NC Mall, then dyed via Dyeworks. (Owls tracks this last part!) - def dyeworks_buyable? - dyeworks_base_buyable? && dyeworks_dyeable? - end - - DYEWORKS_NAME_PATTERN = %r{ - ^( - # Most Dyeworks items have a colon in the name. - Dyeworks\s+(?.+?:)\s*(?.+) - | - # But sometimes they omit it. If so, assume the first word is the color! - Dyeworks\s+(?\S+)\s*(?.+) - )$ - }x - def inferred_dyeworks_base_item - name_match = name.match(DYEWORKS_NAME_PATTERN) - return nil if name_match.nil? - - Item.find_by_name(name_match["base"]) - end - def source if dyeworks_buyable? :dyeworks diff --git a/app/models/item/dyeworks.rb b/app/models/item/dyeworks.rb new file mode 100644 index 00000000..e873275e --- /dev/null +++ b/app/models/item/dyeworks.rb @@ -0,0 +1,57 @@ +class Item + module Dyeworks + def dyeworks? + dyeworks_base_item.present? + end + + # Whether this is a Dyeworks item whose base item can currently be purchased + # in the NC Mall. It may or may not currently be *dyeable* in the NC Mall, + # because Dyeworks eligibility is often a limited-time event. + def dyeworks_base_buyable? + dyeworks_base_item.present? && dyeworks_base_item.currently_in_mall? + end + + # Whether this is one of the few Dyeworks items that can be dyed in the NC + # Mall at any time, rather than as part of a limited-time event. (Owls tracks + # this, not us!) + def dyeworks_permanent? + return false if nc_trade_value.nil? + nc_trade_value.value_text.include?("Permanent Dyeworks") + end + + # Whether this is a Dyeworks item that can be dyed in the NC Mall ~right now, + # but only as part of a limited-time event. (Owls tracks this, not us!) + def dyeworks_limited? + return false if nc_trade_value.nil? + nc_trade_value.value_text.include?("Limited Dyeworks") + end + + # Whether this is a Dyeworks item that can be dyed in the NC Mall ~right now, + # either at any time or as a limited-time event. (Owls tracks this, not us!) + def dyeworks_dyeable? + dyeworks_permanent? || dyeworks_limited? + end + + # Whether this is a Dyeworks item whose base item can currently be purchased + # in the NC Mall, then dyed via Dyeworks. (Owls tracks this last part!) + def dyeworks_buyable? + dyeworks_base_buyable? && dyeworks_dyeable? + end + + DYEWORKS_NAME_PATTERN = %r{ + ^( + # Most Dyeworks items have a colon in the name. + Dyeworks\s+(?.+?:)\s*(?.+) + | + # But sometimes they omit it. If so, assume the first word is the color! + Dyeworks\s+(?\S+)\s*(?.+) + )$ + }x + def inferred_dyeworks_base_item + name_match = name.match(DYEWORKS_NAME_PATTERN) + return nil if name_match.nil? + + Item.find_by_name(name_match["base"]) + end + end +end \ No newline at end of file