dry-struct
dry-struct copied to clipboard
`define_accessors` eval block consumes nonsensical values
Describe the bug
Dry::Struct::ClassInterface#define_accessors naively consumes attribute value in eval block.
If the name contains a single space, second word becomes a parameter.
If argument contains special symbols, SyntaxError
is raised.
To Reproduce
require 'dry-types'
require 'dry-struct'
module Types
include Dry.Types()
end
class Line < Dry::Struct
attribute :'test string', Types::String
end
Line.new('test string': '42').test # => ArgumentError: wrong number of arguments (given 0, expected 1)
class Handlebar < Dry::Struct
attribute :'{test}', Types::String.optional.default(nil)
end # => raises SyntaxError
Expected behavior
Direct accessor methods are only created if the attribute name is a valid method name.
By default, a specific error is raised for names that don't fit the criteria.
Option to skip accessor creation is available.
My environment
- Affects my production application: NO
- Ruby version: 2.6.6, 2.7.3