Module: Buildkite::Pipelines::Attributes::ClassMethods

Defined in:
lib/buildkite/pipelines/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(attribute_name, **options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



58
59
60
# File 'lib/buildkite/pipelines/attributes.rb', line 58

def inherited(subclass)
  subclass.permitted_attributes.merge(permitted_attributes)
end

#permits?(attr) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/buildkite/pipelines/attributes.rb', line 66

def permits?(attr)
  @permitted_attributes.include?(attr.to_s)
end

#permitted_attributesObject



62
63
64
# File 'lib/buildkite/pipelines/attributes.rb', line 62

def permitted_attributes
  @permitted_attributes ||= Set.new
end