Class: Assistant::AudienceUpdateChanges

Inherits:
Object
  • Object
show all
Defined in:
app/services/assistant/audience_update_changes.rb

Overview

Interprets optional audience-update attributes without losing false values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ void

Parameters:

  • attributes (Hash)

    keyword arguments received by the assistant tool



9
10
11
# File 'app/services/assistant/audience_update_changes.rb', line 9

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesHash (readonly)

Returns keyword arguments received by the assistant tool.

Returns:

  • (Hash)

    keyword arguments received by the assistant tool



14
15
16
# File 'app/services/assistant/audience_update_changes.rb', line 14

def attributes
  @attributes
end

Instance Method Details

#assign_to(audience) ⇒ void

This method returns an undefined value.

Parameters:

  • audience (Audience)

    audience being updated



63
64
65
66
# File 'app/services/assistant/audience_update_changes.rb', line 63

def assign_to(audience)
  assign_name_to(audience)
  audience.assign_attributes(dynamic_attributes)
end

#criteriaHash?

Returns customer search criteria from the tool.

Returns:

  • (Hash, nil)

    customer search criteria from the tool



27
28
29
# File 'app/services/assistant/audience_update_changes.rb', line 27

def criteria
  attributes[:customer_search_params]
end

#dynamic_fields_requested?Boolean

Returns whether any dynamic audience field was provided.

Returns:

  • (Boolean)

    whether any dynamic audience field was provided



17
18
19
# File 'app/services/assistant/audience_update_changes.rb', line 17

def dynamic_fields_requested?
  %i[customer_search_params add_all_emails max_members].any? { |key| !attributes[key].nil? }
end

#effective_add_all_emails(audience) ⇒ Boolean

Returns effective add_all_emails value.

Parameters:

  • audience (Audience)

    audience being updated

Returns:

  • (Boolean)

    effective add_all_emails value



49
50
51
52
53
# File 'app/services/assistant/audience_update_changes.rb', line 49

def effective_add_all_emails(audience)
  return audience.add_all_emails.to_b if attributes[:add_all_emails].nil?

  attributes[:add_all_emails].to_b
end

#effective_criteria(audience) ⇒ Hash?

Returns effective criteria (provided or existing).

Parameters:

  • audience (Audience)

    audience being updated

Returns:

  • (Hash, nil)

    effective criteria (provided or existing)



43
44
45
# File 'app/services/assistant/audience_update_changes.rb', line 43

def effective_criteria(audience)
  criteria || audience.customer_search_params
end

#effective_max_members(audience) ⇒ Integer?

Returns effective max members value.

Parameters:

  • audience (Audience)

    audience being updated

Returns:

  • (Integer, nil)

    effective max members value



57
58
59
# File 'app/services/assistant/audience_update_changes.rb', line 57

def effective_max_members(audience)
  attributes[:max_members].nil? ? audience.max_members : attributes[:max_members]
end

#size_changed?(audience) ⇒ Boolean

Returns whether the audience size criteria changed.

Parameters:

  • audience (Audience)

    audience being updated

Returns:

  • (Boolean)

    whether the audience size criteria changed



33
34
35
36
37
38
39
# File 'app/services/assistant/audience_update_changes.rb', line 33

def size_changed?(audience)
  audience.dynamic? && (
    !criteria.nil? ||
    effective_add_all_emails(audience) != audience.add_all_emails.to_b ||
    effective_max_members(audience) != audience.max_members
  )
end

#valid_criteria?Boolean

Returns whether the provided criteria hash is valid.

Returns:

  • (Boolean)

    whether the provided criteria hash is valid



22
23
24
# File 'app/services/assistant/audience_update_changes.rb', line 22

def valid_criteria?
  criteria.nil? || (criteria.is_a?(Hash) && criteria.present?)
end