How can I reduce cyclomatic complexity of validation and refactor it better way?
Class Document
def initialize(opts)
@opts = opts
return ArgumentError unless valid?
end
def valid?
return false if @opts.nil?
return false if @opts[:property_1].nil?
return false if @opts[:property_2].nil?
return false if !@opts[:property_4].nil? && @opts[:property_3].nil?
# etc. 12 times more, otherwise valid will return true
true
end
def usage_example
if @opts[:property_3]
"#{@opts[:property_3]} #{@opts[:property_1]}"
else
"#{@opts[:property_4]} #{@opts[:property_2]}"
end
end
end
I am trying to avoid duplication of return false if
and somehow perhaps list parameters that I want to verify
Possible interpretation of what I trying to reach is "verify set of rules before processing file"