module Zen::Service::Plugins::Attributes

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/zen/service/plugins/attributes.rb, line 8
def initialize(*args)
  @attributes = assert_valid_attributes!(resolve_args!(args))

  super()
end

Public Instance Methods

initialize_clone(*) click to toggle source
Calls superclass method
# File lib/zen/service/plugins/attributes.rb, line 14
def initialize_clone(*)
  super
  @attributes = @attributes.dup unless @attributes.nil?
end
with_attributes(attributes) click to toggle source
# File lib/zen/service/plugins/attributes.rb, line 19
def with_attributes(attributes)
  clone.tap { |copy| copy.attributes.merge!(attributes) }
end

Protected Instance Methods

attributes() click to toggle source
# File lib/zen/service/plugins/attributes.rb, line 23
          def attributes
  @attributes
end

Private Instance Methods

assert_valid_attributes!(actual) click to toggle source
# File lib/zen/service/plugins/attributes.rb, line 49
        def assert_valid_attributes!(actual)
  unexpected = actual.keys - self.class.attributes_list

  raise(ArgumentError, "wrong attributes #{unexpected} given") if unexpected.any?

  actual
end
resolve_args!(args) click to toggle source
# File lib/zen/service/plugins/attributes.rb, line 27
        def resolve_args!(args) # rubocop:disable Metrics/AbcSize
  opts = args.last.is_a?(Hash) ? args.pop : {}
  attributes = {}
  allowed_length = self.class.attributes_list.length

  if args.length > allowed_length
    raise ArgumentError, "wrong number of attributes (given #{args.length}, expected 0..#{allowed_length})"
  end

  args.each_with_index do |value, i|
    attributes[self.class.attributes_list[i]] = value
  end

  opts.each do |name, value|
    raise(ArgumentError, "attribute #{name} has already been provided as parameter") if attributes.key?(name)

    attributes[name] = value
  end

  attributes
end