Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Make `:positional` a kind of attribute

+38 -37
+12 -8
lib/literal/attributable.rb
··· 7 7 8 8 include Literal::Types 9 9 10 - Visibility = Set[false, :private, :protected, :public].freeze 10 + VisibilityOptions = Set[false, :private, :protected, :public].freeze 11 + KindOptions = Set[nil, :positional, :*, :**, :&].freeze 11 12 12 - def attribute(name, type, special = nil, reader: false, writer: false, positional: false, default: nil, &coercion) 13 + def attribute(name, type, kind = nil, reader: false, writer: false, default: nil, &coercion) 13 14 if default && !(Proc === default || default.frozen?) 14 15 raise Literal::ArgumentError.new("The default must be a frozen object or a Proc.") 15 16 end 16 17 17 - unless Visibility.include?(reader) 18 - raise Literal::ArgumentError.new("The reader must be one of #{Visibility.map(&:inspect).join(', ')}.") 18 + unless VisibilityOptions.include?(reader) 19 + raise Literal::ArgumentError.new("The reader must be one of #{VisibilityOptions.map(&:inspect).join(', ')}.") 19 20 end 20 21 21 - unless Visibility.include?(writer) 22 - raise Literal::ArgumentError.new("The writer must be one of #{Visibility.map(&:inspect).join(', ')}.") 22 + unless VisibilityOptions.include?(writer) 23 + raise Literal::ArgumentError.new("The writer must be one of #{VisibilityOptions.map(&:inspect).join(', ')}.") 23 24 end 24 25 25 26 if reader && :class == name ··· 28 29 ) 29 30 end 30 31 32 + unless KindOptions.include?(kind) 33 + raise Literal::ArgumentError.new("The kind must be one of #{KindOptions.map(&:inspect).join(', ')}.") 34 + end 35 + 31 36 attribute = Literal::Attribute.new( 32 37 name:, 33 38 type:, 34 - special:, 39 + kind:, 35 40 reader:, 36 41 writer:, 37 - positional:, 38 42 default:, 39 43 coercion:, 40 44 )
+18 -20
lib/literal/attributable/generators/initializer.rb
··· 19 19 20 20 def params 21 21 @attributes.each_value.map do |attribute| 22 - case attribute.special 22 + case attribute.kind 23 23 when :* 24 24 PositionalSplat.new(attribute:) 25 25 when :** 26 26 KeywordSplat.new(attribute:) 27 27 when :& 28 28 BlockParam.new(attribute:) 29 - else 30 - if attribute.positional 31 - if attribute.default 32 - PositionalParam.new( 33 - name: attribute.name, 34 - default: "Literal::Null", 35 - ) 36 - elsif attribute.type === nil 37 - PositionalParam.new( 38 - name: attribute.name, 39 - default: "nil", 40 - ) 41 - else 42 - PositionalParam.new( 43 - name: attribute.name, 44 - default: nil, 45 - ) 46 - end 29 + when :positional 30 + if attribute.default 31 + PositionalParam.new( 32 + name: attribute.name, 33 + default: "Literal::Null", 34 + ) 35 + elsif attribute.type === nil 36 + PositionalParam.new( 37 + name: attribute.name, 38 + default: "nil", 39 + ) 47 40 else 48 - KeywordParam.new(attribute:) 41 + PositionalParam.new( 42 + name: attribute.name, 43 + default: nil, 44 + ) 49 45 end 46 + else 47 + KeywordParam.new(attribute:) 50 48 end 51 49 end 52 50 end
+3 -4
lib/literal/attribute.rb
··· 3 3 class Literal::Attribute 4 4 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 5 5 6 - def initialize(name:, type:, special:, reader:, writer:, positional:, default:, coercion:) 6 + def initialize(name:, type:, kind:, reader:, writer:, default:, coercion:) 7 7 @name = name 8 8 @type = type 9 - @special = special 9 + @kind = kind 10 10 @reader = reader 11 11 @writer = writer 12 - @positional = positional 13 12 @default = default 14 13 @coercion = coercion 15 14 end 16 15 17 - attr_reader :name, :type, :special, :reader, :writer, :positional, :default, :coercion 16 + attr_reader :name, :type, :kind, :reader, :writer, :default, :coercion 18 17 19 18 def coerce(value, context:) 20 19 context.instance_exec(value, &@coercion)
+2 -2
lib/literal/attributes.test.rb
··· 3 3 class Person 4 4 extend Literal::Attributes 5 5 6 - attribute :name, String, reader: :public 6 + attribute :name, String, :positional, reader: :public 7 7 attribute :age, Integer, reader: :public 8 8 end 9 9 10 10 test do 11 - person = Person.new(name: "John", age: 30) 11 + person = Person.new("John", age: 30) 12 12 13 13 expect(person.name) == "John" 14 14 expect(person.age) == 30
+2 -2
lib/literal/data.rb
··· 2 2 3 3 class Literal::Data < Literal::Structish 4 4 class << self 5 - def attribute(name, type, special = nil, reader: :public, positional: false, default: nil) 6 - super(name, type, special, reader:, writer: false, positional:, default:) 5 + def attribute(name, type, kind = nil, reader: :public, positional: false, default: nil) 6 + super(name, type, kind, reader:, writer: false, positional:, default:) 7 7 end 8 8 9 9 def _load(data)
+1 -1
lib/literal/struct.rb
··· 2 2 3 3 class Literal::Struct < Literal::Structish 4 4 class << self 5 - def attribute(name, type, special = nil, reader: :public, writer: :public, positional: false, default: nil) 5 + def attribute(name, type, kind = nil, reader: :public, writer: :public, positional: false, default: nil) 6 6 super 7 7 end 8 8