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.



98
99
100
# File 'lib/crystalruby/adapter.rb', line 98

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 .crystalize to understand the raw and lib parameters.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/crystalruby/adapter.rb', line 77

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

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

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

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

E.g.

crystalize :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. crystalize :int32, raw: true def add(a: :int32, b: :int32)

"a + b\n"

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 crystalize( returns=:void, raw: false, async: false, lib: "crystalruby", &block)
  (self == TOPLEVEL_BINDING.receiver ? Object : self).instance_eval do
    @crystalize_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 crystalize 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.



58
59
60
61
62
63
64
65
# File 'lib/crystalruby/adapter.rb', line 58

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.



70
71
72
# File 'lib/crystalruby/adapter.rb', line 70

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