Module: Jade::Stdlib

Extended by:
Stdlib
Included in:
Stdlib
Defined in:
lib/jade/stdlib.rb,
lib/jade/stdlib/set.rb,
lib/jade/stdlib/char.rb,
lib/jade/stdlib/dict.rb,
lib/jade/stdlib/list.rb,
lib/jade/stdlib/task.rb,
lib/jade/stdlib/bytes.rb,
lib/jade/stdlib/clock.rb,
lib/jade/stdlib/maybe.rb,
lib/jade/stdlib/tuple.rb,
lib/jade/stdlib/basics.rb,
lib/jade/stdlib/decode.rb,
lib/jade/stdlib/encode.rb,
lib/jade/stdlib/result.rb,
lib/jade/stdlib/string.rb,
lib/jade/stdlib/calendar.rb,
lib/jade/stdlib/compiled.rb,
lib/jade/stdlib/intrinsics.rb,
lib/jade/stdlib/decode/params.rb

Defined Under Namespace

Modules: Basics, Bytes, Calendar, Char, Clock, Compiled, Decode, Dict, Encode, Intrinsics, List, Maybe, Result, Set, String, Task, Tuple

Constant Summary collapse

INTRINSICS =
%w[
  Basics String List Tuple Char Task
  Decode Encode
  Bytes
  Dict Set
].freeze
COMPILED =
%w[Maybe Result Decode.Params Calendar Clock].freeze
TOPLEVELS =
(INTRINSICS + COMPILED).map { it.split('.', 2).first }.to_set.freeze
STDLIBS =
[
  Stdlib::Basics, Stdlib::Maybe, Stdlib::Tuple, Stdlib::List, Stdlib::Char,
  Stdlib::String, Stdlib::Result, Stdlib::Task,
  Stdlib::Dict, Stdlib::Set,
  Stdlib::Decode, Stdlib::Decode::Params, Stdlib::Encode,
  Stdlib::Bytes,
  Stdlib::Calendar, Stdlib::Clock,
]
EXTENSIONS =

Loaded into the registry but not auto-imported into user modules.

[
  Stdlib::Dict, Stdlib::Set,
  Stdlib::Decode, Stdlib::Decode::Params, Stdlib::Encode,
  Stdlib::Calendar, Stdlib::Clock,
]
PRIVATE_CONSTRUCTORS =
%w[Tuple.Tuple2 Tuple.Tuple3 Tuple.Tuple4].freeze

Instance Method Summary collapse

Instance Method Details

#apply(registry) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jade/stdlib.rb', line 51

def apply(registry)
  # TODO: [ModuleLoaderRefactor] This should live in registry probably
  user_entries = registry
    .modules
    .values
    .reject { is_stdlib?(it) }

  user_entries
    .reduce(registry) do |acc, entry|
      add_imports(entry)
        .then { acc.add_module(it) }
    end
end

#is_intrinsic?(entry) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/jade/stdlib.rb', line 76

def is_intrinsic?(entry)
  INTRINSICS.include? entry.name
end

#is_stdlib?(entry) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/jade/stdlib.rb', line 84

def is_stdlib?(entry)
  is_intrinsic?(entry) || COMPILED.include?(entry.name)
end

#load(registry) ⇒ Object



46
47
48
49
# File 'lib/jade/stdlib.rb', line 46

def load(registry)
  registry
    .then { load_stdlib(registry) }
end

#private_constructor?(name) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/jade/stdlib.rb', line 80

def private_constructor?(name)
  PRIVATE_CONSTRUCTORS.include?(name)
end

#requires(name) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/jade/stdlib.rb', line 65

def requires(name)
  return [] if COMPILED.include?(name)

  prefix = '../' * name.count('.')
  COMPILED
    .map { it.downcase.tr('.', '/') }
    .map { "require_relative '#{prefix}#{it}'" }
end

#stdlib_name?(qualified_name) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/jade/stdlib.rb', line 88

def stdlib_name?(qualified_name)
  dot = qualified_name.index('.')
  first = dot ? qualified_name[0, dot] : qualified_name
  TOPLEVELS.include?(first)
end