···11-class Literal::Value
22- class << self
33- attr_reader :__type__
44-55- def define(type, &block)
66- value_class = Class.new(self) do
77- @__type__ = type
88-99- case type
1010- when Literal::Types::_Class(String)
1111- alias_method :to_s, :value
1212- alias_method :to_str, :value
1313- when Literal::Types::_Class(Symbol)
1414- alias_method :to_sym, :value
1515- when Literal::Types::_Class(Integer)
1616- alias_method :to_i, :value
1717- when Literal::Types::_Class(Float)
1818- alias_method :to_f, :value
1919- when Literal::Types::_Class(Set)
2020- alias_method :to_set, :value
2121- when Literal::Types::_Class(Array)
2222- alias_method :to_a, :value
2323- alias_method :to_ary, :value
2424- when Literal::Types::_Class(Hash)
2525- alias_method :to_h, :value
2626- when Literal::Types::_Class(Proc)
2727- alias_method :to_proc, :value
2828- end
2929- end
3030-3131- value_class.class_exec(&block) if block
3232- value_class
3333- end
3434- end
11+# frozen_string_literal: true
35233+class Literal::Value
364 def initialize(value)
375 type = self.class.__type__
386 raise Literal::TypeError, "Expected value: `#{value.inspect}` to be: `#{type.inspect}`." unless type === value
77+398 @value = value.frozen? ? value : value.dup.freeze
409 freeze
4110 end
···4312 attr_reader :value
44134514 def inspect
4646- "#{self.class.name}(#{value.inspect})"
4747- end
1515+ "#{self.class.name}(#{value.inspect})"
1616+ end
1717+1818+ class StringValue < Literal::Value
1919+ alias_method :to_s, :value
2020+ alias_method :to_str, :value
2121+ end
2222+2323+ class SymbolValue < Literal::Value
2424+ alias_method :to_sym, :value
2525+ end
2626+2727+ class IntegerValue < Literal::Value
2828+ alias_method :to_i, :value
2929+ end
3030+3131+ class FloatValue < Literal::Value
3232+ alias_method :to_f, :value
3333+ end
3434+3535+ class SetValue < Literal::Value
3636+ alias_method :to_set, :value
3737+ end
3838+3939+ class ArrayValue < Literal::Value
4040+ alias_method :to_a, :value
4141+ alias_method :to_ary, :value
4242+ end
4343+4444+ class HashValue < Literal::Value
4545+ alias_method :to_h, :value
4646+ end
4747+4848+ class ProcValue < Literal::Value
4949+ alias_method :to_proc, :value
5050+ end
5151+5252+ TYPE_CLASSES = {
5353+ String => StringValue,
5454+ Symbol => SymbolValue,
5555+ Integer => IntegerValue,
5656+ Float => FloatValue,
5757+ Set => SetValue,
5858+ Array => ArrayValue,
5959+ Hash => HashValue,
6060+ Proc => ProcValue
6161+ }
6262+6363+ class << self
6464+ attr_reader :__type__
6565+6666+ def define(type, &block)
6767+ type_class = Class.new(TYPE_CLASSES[type] || self, &block)
6868+ type_class.instance_variable_set(:@__type__, type)
6969+ type_class
7070+ end
7171+ end
4872end
+1-1
literal.gemspec
···1212 spec.description = "Strict Attributes is a gem that allows you to define strict attributes on your models."
1313 spec.homepage = "https://github.com/joeldrapper/literal"
1414 spec.license = "MIT"
1515- spec.required_ruby_version = ">= 2.7"
1515+ spec.required_ruby_version = ">= 3.0"
16161717 spec.metadata["homepage_uri"] = spec.homepage
1818 spec.metadata["source_code_uri"] = "https://github.com/joeldrapper/literal"
+63-54
test/literal/value.rb
···11+# frozen_string_literal: true
22+13let def type = Literal::Value(Integer)
24let def example = type.new(1)
3546context "with Integer" do
55- let def type = Literal::Value(Integer)
66- let def example = type.new(1)
77+ let def value = 1
88+ let def type = Literal::Value(Integer)
99+ let def example = type.new(value)
71088- test "invalid type" do
99- expect { type.new(1.0) }.to_raise(Literal::TypeError)
1010- end
1111+ test "invalid type" do
1212+ expect { type.new(1.0) }.to_raise(Literal::TypeError)
1313+ end
11141212- test do
1313- expect(example.value) == 1
1414- expect(example.to_i) == 1
1515- end
1515+ test do
1616+ expect(example.value) == value
1717+ expect(example.to_i) == value
1818+ end
1619end
17201821context "with String" do
1919- let def type = Literal::Value(String)
2020- let def example = type.new("foo")
2222+ let def value = "foo"
2323+ let def type = Literal::Value(String)
2424+ let def example = type.new(value)
21252222- test do
2323- expect(example.value) == "foo"
2424- expect(example.to_s) == "foo"
2525- expect(example.to_str) == "foo"
2626- end
2626+ test do
2727+ expect(example.value) == value
2828+ expect(example.to_s) == value
2929+ expect(example.to_str) == value
3030+ end
2731end
28322933context "with Symbol" do
3030- let def type = Literal::Value(Symbol)
3131- let def example = type.new(:foo)
3434+ let def value = :foo
3535+ let def type = Literal::Value(Symbol)
3636+ let def example = type.new(value)
32373333- test do
3434- expect(example.value) == :foo
3535- expect(example.to_sym) == :foo
3636- end
3838+ test do
3939+ expect(example.value) == value
4040+ expect(example.to_sym) == value
4141+ end
3742end
38433944context "with Float" do
4040- let def type = Literal::Value(Float)
4141- let def example = type.new(1.0)
4545+ let def value = 1.0
4646+ let def type = Literal::Value(Float)
4747+ let def example = type.new(value)
42484343- test do
4444- expect(example.value) == 1.0
4545- expect(example.to_f) == 1.0
4646- end
4949+ test do
5050+ expect(example.value) == value
5151+ expect(example.to_f) == value
5252+ end
4753end
48544955context "with Set" do
5050- let def type = Literal::Value(Set)
5151- let def example = type.new(Set[1, 2, 3])
5656+ let def value = Set[1, 2, 3]
5757+ let def type = Literal::Value(Set)
5858+ let def example = type.new(value)
52595353- test do
5454- expect(example.value) == Set[1, 2, 3]
5555- expect(example.to_set) == Set[1, 2, 3]
5656- end
6060+ test do
6161+ expect(example.value) == value
6262+ expect(example.to_set) == value
6363+ end
5764end
58655966context "with Array" do
6060- let def type = Literal::Value(Array)
6161- let def example = type.new([1, 2, 3])
6767+ let def value = [1, 2, 3]
6868+ let def type = Literal::Value(Array)
6969+ let def example = type.new(value)
62706363- test do
6464- expect(example.value) == [1, 2, 3]
6565- expect(example.to_a) == [1, 2, 3]
6666- expect(example.to_ary) == [1, 2, 3]
6767- end
7171+ test do
7272+ expect(example.value) == value
7373+ expect(example.to_a) == value
7474+ expect(example.to_ary) == value
7575+ end
6876end
69777078context "with Hash" do
7171- let def type = Literal::Value(Hash)
7272- let def example = type.new({ foo: :bar })
7979+ let def value = { foo: :bar }
8080+ let def type = Literal::Value(Hash)
8181+ let def example = type.new(value)
73827474- test do
7575- expect(example.value) == { foo: :bar }
7676- expect(example.to_h) == { foo: :bar }
7777- end
8383+ test do
8484+ expect(example.value) == value
8585+ expect(example.to_h) == value
8686+ end
7887end
79888089context "with Proc" do
8181- let def type = Literal::Value(Proc)
8282- let def value = -> { :foo }
8383- let def example = type.new(value)
9090+ let def value = -> { :foo }
9191+ let def type = Literal::Value(Proc)
9292+ let def example = type.new(value)
84938585- test do
8686- expect(example.value) == value
8787- expect(example.to_proc) == value
8888- end
9494+ test do
9595+ expect(example.value) == value
9696+ expect(example.to_proc) == value
9797+ end
8998end