@@ -36,7 +36,7 @@ module Adapter
3636 # @option options [Boolean] :async (false) Mark the method as async (allows multiplexing).
3737 # @option options [String] :lib ("crystalruby") The name of the library to compile the Crystal code into.
3838 # @option options [Proc] :block An optional wrapper Ruby block that wraps around any invocations of the crystal code
39- def crystallize ( returns = :void , raw : false , async : false , lib : "crystalruby" , &block )
39+ def crystallize ( returns = :void , raw : false , async : false , lib : "crystalruby" , &block )
4040 ( self == TOPLEVEL_BINDING . receiver ? Object : self ) . instance_eval do
4141 @crystallize_next = {
4242 raw : raw ,
@@ -49,7 +49,7 @@ def crystallize( returns=:void, raw: false, async: false, lib: "crystalruby", &b
4949 end
5050
5151 # Alias for `crystallize`
52- alias : crystalize : crystallize
52+ alias crystalize crystallize
5353
5454 # Exposes a Ruby method to one or more Crystal libraries.
5555 # Type annotations follow the same rules as the `crystallize` method, but are
@@ -58,7 +58,7 @@ def crystallize( returns=:void, raw: false, async: false, lib: "crystalruby", &b
5858 # @param [Hash] options The options hash.
5959 # @option options [Boolean] :raw (false) Pass raw Crystal code to the compiler as a string.
6060 # @option options [String] :libs (["crystalruby"]) The name of the Crystal librarie(s) to expose the Ruby code to.
61- def expose_to_crystal ( returns = :void , libs : [ "crystalruby" ] )
61+ def expose_to_crystal ( returns = :void , libs : [ "crystalruby" ] )
6262 ( self == TOPLEVEL_BINDING . receiver ? Object : self ) . instance_eval do
6363 @expose_next_to_crystal = {
6464 returns : returns ,
@@ -70,22 +70,26 @@ def expose_to_crystal( returns=:void, libs: ["crystalruby"])
7070 # Define a shard dependency
7171 # This dependency will be automatically injected into the shard.yml file for
7272 # the given library and installed upon compile if it is not already installed.
73- def shard ( shard_name , lib : ' crystalruby' , **opts )
73+ def shard ( shard_name , lib : " crystalruby" , **opts )
7474 CrystalRuby ::Library [ lib ] . require_shard ( shard_name , **opts )
7575 end
7676
7777 # Use this method to define inline Crystal code that does not need to be bound to a Ruby method.
7878 # This is useful for defining classes, modules, performing set-up tasks etc.
7979 # See: docs for .crystallize to understand the `raw` and `lib` parameters.
8080 def crystal ( raw : false , lib : "crystalruby" , &block )
81- inline_crystal_body = respond_to? ( :name ) ? Template ::InlineChunk . render (
82- {
83- module_name : name ,
84- body : SourceReader . extract_source_from_proc ( block , raw : raw ) ,
85- mod_or_class : self . kind_of? ( Class ) && self < Types ::Type ? "class" : "module" ,
86- superclass : self . kind_of? ( Class ) && self < Types ::Type ? "< #{ self . crystal_supertype } " : ""
87- } ) :
88- SourceReader . extract_source_from_proc ( block , raw : raw )
81+ inline_crystal_body = if respond_to? ( :name )
82+ Template ::InlineChunk . render (
83+ {
84+ module_name : name ,
85+ body : SourceReader . extract_source_from_proc ( block , raw : raw ) ,
86+ mod_or_class : is_a? ( Class ) && self < Types ::Type ? "class" : "module" ,
87+ superclass : is_a? ( Class ) && self < Types ::Type ? "< #{ crystal_supertype } " : ""
88+ }
89+ )
90+ else
91+ SourceReader . extract_source_from_proc ( block , raw : raw )
92+ end
8993
9094 CrystalRuby ::Library [ lib ] . crystallize_chunk (
9195 self ,
@@ -94,7 +98,6 @@ def crystal(raw: false, lib: "crystalruby", &block)
9498 )
9599 end
96100
97-
98101 # This method provides a useful DSL for defining Crystal types in pure Ruby
99102 # MyType = CRType{ Int32 | Hash(String, Array(Bool) | Float65 | Nil) }
100103 # @param [Proc] block The block within which we build the type definition.
@@ -152,14 +155,16 @@ def #{method.name} (#{(args.keys - [:__yield_to]).join(", ")})
152155
153156 owner = method . owner . singleton_class? ? method . owner . attached_object : method . owner
154157 owner . class_eval ( src )
155- owner . instance_eval ( src ) unless method . kind_of? ( UnboundMethod ) && method . owner . ancestors . include? ( CrystalRuby ::Types ::Type )
156- method = owner . send ( method . kind_of? ( UnboundMethod ) ? :instance_method : :method , method . name )
158+ unless method . is_a? ( UnboundMethod ) && method . owner . ancestors . include? ( CrystalRuby ::Types ::Type )
159+ owner . instance_eval ( src )
160+ end
161+ method = owner . send ( method . is_a? ( UnboundMethod ) ? :instance_method : :method , method . name )
157162
158163 libs . each do |lib |
159164 CrystalRuby ::Library [ lib ] . expose_method (
160165 method ,
161166 args ,
162- returns ,
167+ returns
163168 )
164169 end
165170 end
0 commit comments