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!
This commit is contained in:
Emi Matchu 2024-06-18 15:21:43 -07:00
parent 26dfe13b0e
commit 015010345a
2 changed files with 58 additions and 54 deletions

View file

@ -3,6 +3,7 @@ require "async/barrier"
class Item < ApplicationRecord class Item < ApplicationRecord
include PrettyParam include PrettyParam
include Item::Dyeworks
# We use the `type` column to mean something other than what Rails means! # We use the `type` column to mean something other than what Rails means!
self.inheritance_column = nil self.inheritance_column = nil
@ -198,60 +199,6 @@ class Item < ApplicationRecord
nc_mall_record.present? nc_mall_record.present?
end 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+(?<color>.+?:)\s*(?<base>.+)
|
# But sometimes they omit it. If so, assume the first word is the color!
Dyeworks\s+(?<color>\S+)\s*(?<base>.+)
)$
}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 def source
if dyeworks_buyable? if dyeworks_buyable?
:dyeworks :dyeworks

View file

@ -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+(?<color>.+?:)\s*(?<base>.+)
|
# But sometimes they omit it. If so, assume the first word is the color!
Dyeworks\s+(?<color>\S+)\s*(?<base>.+)
)$
}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