Module: CrystalRuby::Adapter

Defined in:
lib/crystalruby/adapter.rb

Instance Method Summary collapse

Instance Method Details

#CRType(&block) ⇒ Object

This method provides a useful DSL for defining Crystal types in pure Ruby MyType = CRType{ Int32 | Hash(String, Array(Bool) | Float65 | Nil) }

Parameters:

  • block (Proc)

    The block within which we build the type definition.



104
105
106
# File 'lib/crystalruby/adapter.rb', line 104

def CRType(&block)
  TypeBuilder.build_from_source(block, context: self)
end

#crystal(raw: false, lib: "crystalruby", &block) ⇒ Object

Use this method to define inline Crystal code that does not need to be bound to a Ruby method. This is useful for defining classes, modules, performing set-up tasks etc. See: docs for .crystallize to understand the ‘raw` and `lib` parameters.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/crystalruby/adapter.rb', line 80

def crystal(raw: false, lib: "crystalruby", &block)
  inline_crystal_body = if respond_to?(:name)
                          Template::InlineChunk.render(
                            {
                              module_name: name,
                              body: SourceReader.extract_source_from_proc(block, raw: raw),
                              mod_or_class: is_a?(Class) && self < Types::Type ? "class" : "module",
                              superclass: is_a?(Class) && self < Types::Type ? "< #{crystal_supertype}" : ""
                            }
                          )
                        else
                          SourceReader.extract_source_from_proc(block, raw: raw)
                        end

  CrystalRuby::Library[lib].crystallize_chunk(
    self,
    Digest::MD5.hexdigest(inline_crystal_body),
    inline_crystal_body
  )
end

#crystallize(returns = :void, raw: false, async: false, lib: "crystalruby", &block) ⇒ Object Also known as: crystalize

Use this method to annotate a Ruby method that should be crystallized. Compilation and attachment of the method is done lazily. You can force compilation by calling ‘CrystalRuby.compile!` It’s important that all code using crystallized methods is loaded before any manual calls to compile.

E.g.

crystallize :int32 def add(a: :int32, b: :int32)

a + b

end

Pass ‘raw: true` to pass Raw crystal code to the compiler as a string instead. (Useful for cases where the Crystal method body is not valid Ruby) E.g. crystallize :int32, raw: true def add(a: :int32, b: :int32)

<<~CRYSTAL
a + b
CRYSTAL

end

Pass ‘async: true` to make the method async. Crystal methods will always block the currently executing Ruby thread. With async: false, all other Crystal code will be blocked while this Crystal method is executing (similar to Ruby code with the GVL) With async: true, several Crystal methods can be executing concurrently.

Pass lib: “name_of_lib” to compile Crystal code into several distinct libraries. This can help keep compilation times low, by packaging your Crystal code into separate shared objects.

Parameters:

  • returns (defaults to: :void)

    The return type of the method. Optional (defaults to :void).

  • options (Hash)

    The options hash.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crystalruby/adapter.rb', line 39

def crystallize(returns = :void, raw: false, async: false, lib: "crystalruby", &block)
  (self == TOPLEVEL_BINDING.receiver ? Object : self).instance_eval do
    @crystallize_next = {
      raw: raw,
      async: async,
      returns: returns,
      block: block,
      lib: lib
    }
  end
end

#expose_to_crystal(returns = :void, libs: ["crystalruby"]) ⇒ Object

Exposes a Ruby method to one or more Crystal libraries. Type annotations follow the same rules as the ‘crystallize` method, but are applied in reverse.

Parameters:

  • returns (defaults to: :void)

    The return type of the method. Optional (defaults to :void).

  • options (Hash)

    The options hash.



61
62
63
64
65
66
67
68
# File 'lib/crystalruby/adapter.rb', line 61

def expose_to_crystal(returns = :void, libs: ["crystalruby"])
  (self == TOPLEVEL_BINDING.receiver ? Object : self).instance_eval do
    @expose_next_to_crystal = {
      returns: returns,
      libs: libs
    }
  end
end

#shard(shard_name, lib: "crystalruby", **opts) ⇒ Object

Define a shard dependency This dependency will be automatically injected into the shard.yml file for the given library and installed upon compile if it is not already installed.



73
74
75
# File 'lib/crystalruby/adapter.rb', line 73

def shard(shard_name, lib: "crystalruby", **opts)
  CrystalRuby::Library[lib].require_shard(shard_name, **opts)
end