Module: Attio::Concerns::TimeFilterable::ClassMethods

Defined in:
lib/attio/concerns/time_filterable.rb

Instance Method Summary collapse

Instance Method Details

#activity_metrics(period, **opts) ⇒ Hash

Get activity metrics for a period

Parameters:

Returns:

  • (Hash)

    Metrics about records in the period



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/attio/concerns/time_filterable.rb', line 95

def activity_metrics(period, **opts)
  created = in_period(period, date_field: :created_at, **opts)
  updated = in_period(period, date_field: :updated_at, **opts)

  {
    period: period.label,
    created_count: created.size,
    updated_count: updated.size,
    total_activity: (created + updated).uniq.size
  }
end

#created_in_month(year, month, **opts) ⇒ Array

Get records created in a specific month

Parameters:

  • year (Integer)

    The year

  • month (Integer)

    The month (1-12)

Returns:

  • (Array)

    Records created in that month



73
74
75
# File 'lib/attio/concerns/time_filterable.rb', line 73

def created_in_month(year, month, **opts)
  in_period(Util::TimePeriod.month(year, month), date_field: :created_at, **opts)
end

#created_in_quarter(year, quarter, **opts) ⇒ Array

Get records created in a specific quarter

Parameters:

  • year (Integer)

    The year

  • quarter (Integer)

    The quarter (1-4)

Returns:

  • (Array)

    Records created in that quarter



81
82
83
# File 'lib/attio/concerns/time_filterable.rb', line 81

def created_in_quarter(year, quarter, **opts)
  in_period(Util::TimePeriod.quarter(year, quarter), date_field: :created_at, **opts)
end

#created_in_year(year, **opts) ⇒ Array

Get records created in a specific year

Parameters:

  • year (Integer)

    The year

Returns:

  • (Array)

    Records created in that year



88
89
90
# File 'lib/attio/concerns/time_filterable.rb', line 88

def created_in_year(year, **opts)
  in_period(Util::TimePeriod.year(year), date_field: :created_at, **opts)
end

#created_this_month(**opts) ⇒ Array

Get records created this month

Returns:

  • (Array)

    Records created in current month



59
60
61
# File 'lib/attio/concerns/time_filterable.rb', line 59

def created_this_month(**opts)
  in_period(Util::TimePeriod.current_month, date_field: :created_at, **opts)
end

#created_this_year(**opts) ⇒ Array

Get records created this year

Returns:

  • (Array)

    Records created in current year



53
54
55
# File 'lib/attio/concerns/time_filterable.rb', line 53

def created_this_year(**opts)
  in_period(Util::TimePeriod.current_year, date_field: :created_at, **opts)
end

#created_year_to_date(**opts) ⇒ Array

Get records created year to date

Returns:

  • (Array)

    Records created YTD



65
66
67
# File 'lib/attio/concerns/time_filterable.rb', line 65

def created_year_to_date(**opts)
  in_period(Util::TimePeriod.year_to_date, date_field: :created_at, **opts)
end

#in_period(period, date_field: :created_at, **opts) ⇒ Array

Filter records by a time period for a specific date field

Parameters:

  • period (Util::TimePeriod)

    The time period to filter by

  • date_field (Symbol) (defaults to: :created_at)

    The field to check (default: :created_at)

Returns:

  • (Array)

    Records within the period



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/attio/concerns/time_filterable.rb', line 19

def in_period(period, date_field: :created_at, **opts)
  all(**opts).select do |record|
    # Try accessor method first, then bracket notation
    date_value = if record.respond_to?(date_field)
      record.send(date_field)
    else
      record[date_field]
    end

    if date_value
      parsed_date = date_value.is_a?(String) ? Time.parse(date_value) : date_value
      period.includes?(parsed_date)
    else
      false
    end
  end
end

#recently_created(days = 7, **opts) ⇒ Array

Get records created in the last N days

Parameters:

  • days (Integer) (defaults to: 7)

    Number of days to look back

Returns:

  • (Array)

    Recently created records



40
41
42
# File 'lib/attio/concerns/time_filterable.rb', line 40

def recently_created(days = 7, **opts)
  in_period(Util::TimePeriod.last_days(days), date_field: :created_at, **opts)
end

#recently_updated(days = 7, **opts) ⇒ Array

Get records updated in the last N days

Parameters:

  • days (Integer) (defaults to: 7)

    Number of days to look back

Returns:

  • (Array)

    Recently updated records



47
48
49
# File 'lib/attio/concerns/time_filterable.rb', line 47

def recently_updated(days = 7, **opts)
  in_period(Util::TimePeriod.last_days(days), date_field: :updated_at, **opts)
end