module Buildkite::Pipelines::Attributes::ClassMethods

Public Instance Methods

attribute(attribute_name, **options) click to toggle source
# File lib/buildkite/pipelines/attributes.rb, line 70
def attribute(attribute_name, **options)
  unless permitted_attributes.add?(attribute_name.to_s)
    raise "Step already defined attribute: #{attribute_name}"
  end

  method_name = options.fetch(:as, attribute_name)

  # Define the main helper method that sets or appends the attribute value.
  define_method(method_name) do |*value|
    if value.empty?
      get(attribute_name)
    elsif options[:append]
      append(attribute_name, *value)
    else
      set(attribute_name, *value)
    end
  end

  # Define a helper method that is equivalent to `||=` or `Set#add?`. It will
  # set the attribute if it hasn't been already set. It will return true/false
  # for whether or not the value was set.
  define_method("#{method_name}?") do |*args|
    if args.empty?
      raise ArgumentError, "`#{method_name}?` must be called with arguments"
    elsif has?(method_name)
      false
    else
      public_send(method_name, *args)
      true
    end
  end

  if options[:append]
    # If this attribute appends by default, then provide a bang(!) helper method
    # that allows you to clear and set the value in one go.
    define_method("#{method_name}!") do |*args|
      unset(attribute_name)
      public_send(method_name, *args)
    end
  end

  Helpers.prepend_attribute_helper(self, attribute_name)
end
inherited(subclass) click to toggle source
# File lib/buildkite/pipelines/attributes.rb, line 58
def inherited(subclass)
  subclass.permitted_attributes.merge(permitted_attributes)
end
permits?(attr) click to toggle source
# File lib/buildkite/pipelines/attributes.rb, line 66
def permits?(attr)
  @permitted_attributes.include?(attr.to_s)
end
permitted_attributes() click to toggle source
# File lib/buildkite/pipelines/attributes.rb, line 62
def permitted_attributes
  @permitted_attributes ||= Set.new
end