Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

feat: Make data structs implicitly Hash coercible (#318)

This allows using `Literal::Data` (and its friends) like so:

``` ruby
class Person < Literal::Data
prop :first_name, String
prop :last_name, String
end

john = Person.new(first_name: "John", last_name: "Doe")
jane = Person.new(**john, first_name: "Jane")
```

This is especially useful when we use Data/Struct as configuration
options data:

``` ruby
class Shrine
module Plugins
module Example
class Options < Literal::Data
# ...
end

DEFAULT_OPTIONS = { ... }

def self.configure(uploader, **options)
uploader.opts[:example] = Options.new(
**DEFAULT_OPTIONS,
**uploader.opts[:example],
**options
)
end
end

register_plugin(:example, Example)
end
end
```

authored by

Alexey Zapparov and committed by
GitHub
6d4d4348 a1aeab6b

+11
+2
lib/literal/data_structure.rb
··· 14 14 {} 15 15 end 16 16 17 + alias to_hash to_h 18 + 17 19 def deconstruct 18 20 to_h.values 19 21 end
+2
lib/literal/properties.rb
··· 92 92 {} 93 93 end 94 94 95 + alias to_hash to_h 96 + 95 97 set_temporary_name "Literal::Properties(Extension)" if respond_to?(:set_temporary_name) 96 98 end 97 99 end
+1
lib/literal/properties/schema.rb
··· 78 78 end 79 79 80 80 buffer << " }\n" << "end\n" 81 + buffer << "alias to_hash to_h\n" 81 82 end 82 83 83 84 def generate_hash(buffer = +"")
+6
test/data.test.rb
··· 53 53 assert_equal(person.deconstruct_keys([:name]), { name: "John" }) 54 54 end 55 55 56 + test "can be implicitly coerced to Hash" do 57 + person = Person.new(name: "John") 58 + 59 + assert_equal({ last_name: "Doe" }.merge(person), { last_name: "Doe", name: "John" }) 60 + end 61 + 56 62 test "can be used as a hash key" do 57 63 person = Person.new(name: "John") 58 64 person2 = Person.new(name: "Bob")