# frozen_string_literal: true

module Onuro
  # Builder class that allows to create a builder pattern for easily creation of
  # the Onuro::Context containing attributes to help you in the event rule processing
  # @see Onuro::Context
  class ContextBuilder
    # Onuro::Context generated by the Onuro::ContextBuilder
    attr_accessor :context

    # Creates a new instance of Onuro::ContextBuilder with its Onuro::Context
    # attribute set to default values.
    #
    # @see Onuro::Context
    def initialize
      self.context = Context.new
    end

    # Adds a key/value item in the Onuro::Context
    #
    # @Example
    #   ContextBuilder.new.add(:member_id, 12)
    #
    # @see Onuro::Context
    def add(key, value)
      context.add(key, value)
    end

    # Class method that uses the builder pattern in order to easy create an Onuro::Context
    # instance and being populated
    #
    # @Example
    #   ContextBuilder.build do |builder|
    #     builder.add(:member_id, 12)
    #   end
    #
    # @see Onuro::Context
    def self.build
      builder = new
      yield builder if block_given?
      builder.context
    end
  end
end
