Class: Whatsapp::MessageTemplates::ComponentSet
- Inherits:
-
Object
- Object
- Whatsapp::MessageTemplates::ComponentSet
- Includes:
- ValueObject
- Defined in:
- lib/ruby/whatsapp/message_templates/component_set.rb
Overview
A template's components, plus every rule that spans more than one of them.
This is the other half of the split described in Component: a component class validates itself, and everything about how components relate lives here. That keeps rules like "a footer is forbidden alongside a limited-time offer" in one place instead of duplicated across the sibling classes that would each only see half the picture.
It is a separate object from Template because editing a template replaces its
components wholesale without re-supplying a name or language, so update needs
exactly these checks and none of the template-identity ones.
category is optional for that reason: when it is absent — as when editing — the
category-dependent rules are skipped rather than guessed at.
Defined Under Namespace
Modules: Defaults
Constant Summary collapse
- SINGLETON_TYPES =
Wire types that may appear at most once in a template.
[ Component::Header::Defaults::TYPE, Component::Footer::Defaults::TYPE, Component::Buttons::Defaults::TYPE, Component::Carousel::Defaults::TYPE, Component::LimitedTimeOffer::Defaults::TYPE, ].freeze
- CATEGORY_RESTRICTED_TYPES =
Components restricted to a subset of categories, and which ones.
{ Component::Carousel::Defaults::TYPE => [Categories::MARKETING].freeze, Component::LimitedTimeOffer::Defaults::TYPE => [Categories::MARKETING].freeze, }.freeze
- LIMITED_TIME_OFFER_HEADER_FORMATS =
Header formats a limited-time-offer template may use.
[ Component::Header::Formats::IMAGE, Component::Header::Formats::VIDEO, ].freeze
- LOCATION_HEADER_CATEGORIES =
Categories that may use a location header.
[Categories::UTILITY, Categories::MARKETING].freeze
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#api_types ⇒ Array<String>
The wire types present, in declared order.
- #find(api_type) ⇒ Component::Base?
-
#initialize(components:, category: nil, parameter_format: ParameterFormats::POSITIONAL) ⇒ ComponentSet
constructor
@raise [ActiveModel::ValidationError] if validation fails.
-
#serialize ⇒ Array<Hash>
The serialized components.
Methods included from ValueObject
Constructor Details
#initialize(components:, category: nil, parameter_format: ParameterFormats::POSITIONAL) ⇒ ComponentSet
@raise [ActiveModel::ValidationError] if validation fails. @raise [TemplateError] if a component type is unknown.
79 80 81 82 83 84 85 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 79 def initialize(components:, category: nil, parameter_format: ParameterFormats::POSITIONAL) @category = Categories.normalize(category) @parameter_format = ParameterFormats.normalize(parameter_format) @components = build_components(components) validate! end |
Instance Attribute Details
#category ⇒ String?
59 60 61 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 59 def category @category end |
#components ⇒ Array<Component::Base>
55 56 57 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 55 def components @components end |
#parameter_format ⇒ String
63 64 65 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 63 def parameter_format @parameter_format end |
Instance Method Details
#api_types ⇒ Array<String>
Returns The wire types present, in declared order.
94 95 96 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 94 def api_types components.map(&:api_type) end |
#find(api_type) ⇒ Component::Base?
89 90 91 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 89 def find(api_type) components.find { |component| component.api_type == api_type } end |
#serialize ⇒ Array<Hash>
Returns The serialized components.
99 100 101 |
# File 'lib/ruby/whatsapp/message_templates/component_set.rb', line 99 def serialize components.map(&:serialize) end |