1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 05:09:38 +02:00
Maybe/app/models/series.rb
Zach Gollwitzer d75be2282b
New Design System + Codebase Refresh (#1823)
Since the very first 0.1.0-alpha.1 release, we've been moving quickly to add new features to the Maybe app. In doing so, some parts of the codebase have become outdated, unnecessary, or overly-complex as a natural result of this feature prioritization.

Now that "core" Maybe is complete, we're moving into a second phase of development where we'll be working hard to improve the accuracy of existing features and build additional features on top of "core". This PR is a quick overhaul of the existing codebase aimed to:

- Establish the brand new and simplified dashboard view (pictured above)
- Establish and move towards the conventions introduced in Cursor rules and project design overview #1788
- Consolidate layouts and improve the performance of layout queries
- Organize the core models of the Maybe domain (i.e. Account::Entry, Account::Transaction, etc.) and break out specific traits of each model into dedicated concerns for better readability
- Remove stale / dead code from codebase
- Remove overly complex code paths in favor of simpler ones
2025-02-21 11:57:59 -05:00

57 lines
1.5 KiB
Ruby

class Series
attr_reader :start_date, :end_date, :interval, :trend, :values
Value = Struct.new(
:date,
:date_formatted,
:trend,
keyword_init: true
)
class << self
def from_raw_values(values, interval: "1 day")
raise ArgumentError, "Must be an array of at least 2 values" unless values.size >= 2
raise ArgumentError, "Must have date and value properties" unless values.all? { |value| value.has_key?(:date) && value.has_key?(:value) }
ordered = values.sort_by { |value| value[:date] }
start_date = ordered.first[:date]
end_date = ordered.last[:date]
new(
start_date: start_date,
end_date: end_date,
interval: interval,
trend: Trend.new(
current: ordered.last[:value],
previous: ordered.first[:value]
),
values: [ nil, *ordered ].each_cons(2).map do |prev_value, curr_value|
Value.new(
date: curr_value[:date],
date_formatted: I18n.l(curr_value[:date], format: :long),
trend: Trend.new(
current: curr_value[:value],
previous: prev_value&.[](:value)
)
)
end
)
end
end
def initialize(start_date:, end_date:, interval:, trend:, values:)
@start_date = start_date
@end_date = end_date
@interval = interval
@trend = trend
@values = values
end
def current
values.last.trend.current
end
def any?
values.any?
end
end