Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Initial sketch for `Literal::Operation`

+124 -60
+1 -5
lib/literal/attributes.rb
··· 25 25 } 26 26 27 27 #{ 28 - __schema__.map { |n, _t| 28 + __schema__.each_key.map { |n| 29 29 "@#{n} = #{n}" 30 30 }.join("\n") 31 31 } ··· 65 65 return @__schema__ if defined?(@__schema__) 66 66 67 67 @__schema__ = superclass.is_a?(self) ? superclass.__schema__.dup : {} 68 - end 69 - 70 - def self.extended(base) 71 - base.include(Literal::Initializer) 72 68 end 73 69 end
-13
lib/literal/initializer.rb
··· 1 - # frozen_string_literal: true 2 - 3 - module Literal::Initializer 4 - def initialize(**attributes) 5 - self.class.__attributes__.each do |name| 6 - attributes[name] ||= nil 7 - end 8 - 9 - attributes.each do |name, value| 10 - send("#{name}=", value) 11 - end 12 - end 13 - end
+50
lib/literal/operation.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Operation 4 + extend Literal::Types 5 + 6 + class << self 7 + def call(...) = new(...).call 8 + 9 + def attribute(name, type) 10 + __schema__[name] = type 11 + 12 + class_eval <<~RUBY, __FILE__, __LINE__ + 1 13 + # frozen_string_literal: true 14 + 15 + def initialize(#{ 16 + __schema__.map { |n, t| 17 + "#{n}: #{t.nil? ? 'nil' : ''}" 18 + }.join(', ') 19 + }) 20 + @__schema__ = self.class.__schema__ 21 + 22 + #{ 23 + __schema__.each_key.map { |n| 24 + "raise ::Literal::TypeError unless @__schema__[:#{n}] === #{n}" 25 + }.join("\n") 26 + } 27 + 28 + #{ 29 + __schema__.each_key.map { |n| 30 + "@#{n} = #{n}" 31 + }.join("\n") 32 + } 33 + end 34 + RUBY 35 + 36 + name 37 + end 38 + 39 + def __schema__ 40 + return @__schema__ if defined?(@__schema__) 41 + 42 + @__schema__ = superclass.is_a?(self) ? superclass.__schema__.dup : {} 43 + end 44 + end 45 + 46 + def to_proc 47 + me = self 48 + proc { me.call } 49 + end 50 + end
+13 -42
lib/literal/value.rb
··· 2 2 3 3 class Literal::Value 4 4 def initialize(value) 5 - type = self.class.__type__ 6 - raise Literal::TypeError, "Expected value: `#{value.inspect}` to be: `#{type.inspect}`." unless type === value 5 + unless __type__ === value 6 + raise Literal::TypeError, 7 + "Expected value: `#{value.inspect}` to be: `#{__type__.inspect}`." 8 + end 7 9 8 10 @value = value.frozen? ? value : value.dup.freeze 11 + 9 12 freeze 10 13 end 11 14 12 15 attr_reader :value 13 16 14 - def inspect 15 - "#{self.class.name}(#{value.inspect})" 16 - end 17 - 18 - class StringValue < Literal::Value 19 - alias_method :to_s, :value 20 - alias_method :to_str, :value 21 - end 17 + def inspect = "#{self.class.name}(#{value.inspect})" 22 18 23 - class SymbolValue < Literal::Value 24 - alias_method :to_sym, :value 25 - end 26 - 27 - class IntegerValue < Literal::Value 28 - alias_method :to_i, :value 29 - end 19 + private 30 20 31 - class FloatValue < Literal::Value 32 - alias_method :to_f, :value 33 - end 34 - 35 - class SetValue < Literal::Value 36 - alias_method :to_set, :value 37 - end 38 - 39 - class ArrayValue < Literal::Value 40 - alias_method :to_a, :value 41 - alias_method :to_ary, :value 42 - end 43 - 44 - class HashValue < Literal::Value 45 - alias_method :to_h, :value 46 - end 47 - 48 - class ProcValue < Literal::Value 49 - alias_method :to_proc, :value 50 - end 21 + def __type__ = self.class.__type__ 51 22 52 23 TYPE_CLASSES = { 24 + Set => SetValue, 25 + Proc => ProcValue, 26 + Hash => HashValue, 27 + Array => ArrayValue, 28 + Float => FloatValue, 53 29 String => StringValue, 54 30 Symbol => SymbolValue, 55 31 Integer => IntegerValue, 56 - Float => FloatValue, 57 - Set => SetValue, 58 - Array => ArrayValue, 59 - Hash => HashValue, 60 - Proc => ProcValue 61 32 } 62 33 63 34 class << self
+8
lib/literal/value/array_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::ArrayValue < Literal::Value 4 + def __type__ = Array 5 + 6 + alias_method :to_a, :value 7 + alias_method :to_ary, :value 8 + end
+7
lib/literal/value/float_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::FloatValue < Literal::Value 4 + def __type__ = Float 5 + 6 + alias_method :to_f, :value 7 + end
+8
lib/literal/value/hash_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::HashValue < Literal::Value 4 + def __type__ = Hash 5 + 6 + alias_method :to_h, :value 7 + alias_method :to_hash, :value 8 + end
+8
lib/literal/value/integer_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::IntegerValue < Literal::Value 4 + def __type__ = Integer 5 + 6 + alias_method :to_i, :value 7 + alias_method :to_int, :value 8 + end
+7
lib/literal/value/proc_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::ProcValue < Literal::Value 4 + def __type__ = Proc 5 + 6 + alias_method :to_proc, :value 7 + end
+7
lib/literal/value/set_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::SetValue < Literal::Value 4 + def __type__ = Set 5 + 6 + alias_method :to_set, :value 7 + end
+8
lib/literal/value/string_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::StringValue < Literal::Value 4 + def __type__ = String 5 + 6 + alias_method :to_s, :value 7 + alias_method :to_str, :value 8 + end
+7
lib/literal/value/symbol_value.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Value::SymbolValue < Literal::Value 4 + def __type__ = Symbol 5 + 6 + alias_method :to_sym, :value 7 + end