···7788 include Literal::Types
991010- Visibility = [:private, :protected, :public].freeze
1010+ Visibility = Set[false, :private, :protected, :public].freeze
11111212 def attribute(name, type, special = nil, reader: false, writer: false, positional: false, default: nil, &coercion)
1313 if default && !(Proc === default || default.frozen?)
1414- raise Literal::ArgumentError.new("The `default` must be a frozen value or a Proc.")
1414+ raise Literal::ArgumentError.new("The default must be a frozen object or a Proc.")
1515 end
16161717- unless false == reader || Visibility.include?(reader)
1818- raise Literal::ArgumentError.new("The `reader` must be one of #{Visibility.map(&:inspect).join(', ')}.")
1717+ unless Visibility.include?(reader)
1818+ raise Literal::ArgumentError.new("The reader must be one of #{Visibility.map(&:inspect).join(', ')}.")
1919 end
20202121- unless false == writer || Visibility.include?(writer)
2222- raise Literal::ArgumentError.new("The `writer` must be one of #{Visibility.map(&:inspect).join(', ')}.")
2323- end
2424-2525- if special && positional
2626- raise Literal::ArgumentError.new("The #{name} attribute cannot be #{special} and positional.")
2121+ unless Visibility.include?(writer)
2222+ raise Literal::ArgumentError.new("The writer must be one of #{Visibility.map(&:inspect).join(', ')}.")
2723 end
28242929- if :class == name && reader
3030- raise Literal::ArgumentError.new("The `:class` attribute should not be defined as a reader because it breaks Ruby's `Object#class` method, which Literal itself depends on.")
2525+ if reader && :class == name
2626+ raise Literal::ArgumentError.new(
2727+ "The `:class` attribute should not be defined as a reader because it breaks Ruby's `Object#class` method, which Literal itself depends on.",
2828+ )
3129 end
32303331 attribute = Literal::Attribute.new(
···4240 )
43414442 literal_attributes[name] = attribute
4545-4646- include literal_extension
4747-4843 define_literal_methods(attribute)
4444+ include literal_extension
4945 end
50465147 def inherited(subclass)
···74707571 #{generate_literal_initializer}
76727777- #{generate_literal_reader(attribute) if attribute.reader?}
7373+ #{generate_literal_reader(attribute) if attribute.reader}
78747979- #{generate_literal_writer(attribute) if attribute.writer?}
7575+ #{generate_literal_writer(attribute) if attribute.writer}
8076 RUBY
8177 end
8278
+4-4
lib/literal/attributable/formatter.rb
···4545 end
46464747 def DefaultAssignment(node)
4848- text "if Literal::Null == #{node.attribute.escaped}"
4848+ text "if Literal::Null == #{node.attribute.escaped_name}"
49495050 indent do
5151- text "#{node.attribute.escaped} = @literal_attributes[:#{node.attribute.name}].default_value"
5151+ text "#{node.attribute.escaped_name} = @literal_attributes[:#{node.attribute.name}].default_value"
5252 end
53535454 newline
···7373 end
74747575 def KeywordEscape(node)
7676- text "#{node.attribute.escaped} = binding.local_variable_get(:#{node.attribute.name})"
7676+ text "#{node.attribute.escaped_name} = binding.local_variable_get(:#{node.attribute.name})"
7777 end
78787979 def KeywordParam(node)
8080- if node.attribute.default?
8080+ if node.attribute.default
8181 text "#{node.attribute.name}: Literal::Null"
8282 elsif node.attribute.type === nil
8383 text "#{node.attribute.name}: nil"
···2626 collection: Ref.new("@attributes"),
2727 key: Symbol.new(attribute.name),
2828 ),
2929- right: Ref.new(attribute.escaped),
2929+ right: Ref.new(attribute.escaped_name),
3030 )
3131 end
3232 end
+3-23
lib/literal/attribute.rb
···11# frozen_string_literal: true
2233class Literal::Attribute
44- RUBY_KEYWORDS = %i[alias and begin break case class def do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield].to_h { |k| [k, "___#{k}___"] }.freeze
44+ RUBY_KEYWORDS = %i[alias and begin break case class def do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield].to_h { |k| [k, "__#{k}__"] }.freeze
5566 def initialize(name:, type:, special:, reader:, writer:, positional:, default:, coercion:)
77 @name = name
···16161717 attr_reader :name, :type, :special, :reader, :writer, :positional, :default, :coercion
18181919- def reader?
2020- !!@reader
2121- end
2222-2323- def writer?
2424- !!@writer
2525- end
2626-2727- def default?
2828- nil != @default
2929- end
3030-3131- def positional?
3232- !!@positional
3333- end
3434-3535- def coercion?
3636- !!@coercion
3737- end
3838-3919 def coerce(value, context:)
4020 context.instance_exec(value, &@coercion)
4121 end
42224343- def escape?
2323+ def ruby_keyword?
4424 !!RUBY_KEYWORDS[@name]
4525 end
46264747- def escaped
2727+ def escaped_name
4828 RUBY_KEYWORDS[@name] || @name
4929 end
5030