I have a simple class that, on initialization, takes between one and eight parameters. It sets the accessors to these to use later. Rubocop is trying to arrest me for the ABC being too high, but I'm not sure if there's actually anything wrong with what I've done. Is this a case where I just disable the inspection on my initialize?
class Foo
attr_accessor :one, :two, :three, :four
attr_accessor :five, :six, :seven, :eight
def initialize(p={})
@one = p[:one] if p[:one].present?
# ...
@eight = p[:eight] if p[:eight].present?
end
end
My only thought on reducing size would be to do something like iterating through all my attr_accessors on initialize, seeing if there is a corresponding symbol passed through in the has, and if so assigning it.
class Foo
attr_accessor :one, :two, :three, :four
attr_accessor :five, :six, :seven, :eight
def initialize(p={})
instance_variables.each do |variable|
send("@#{variable}") = p["#{send(variable)}".to_sym] if p["#{send(variable)}".to_sym].present?
end
end
end
But this seems kind of weak.