Module: Temill::TemillImpl

Included in:
Temill, Temill
Defined in:
lib/temill/core.rb

Overview

Implementation of Temill: this module will be both included and extended to Temill

Instance Method Summary collapse

Instance Method Details

#_initialize(**options) ⇒ Object



35
36
37
38
# File 'lib/temill/core.rb', line 35

def _initialize(**options)
  @options = DefaultOptions.merge(options)
  reset
end

#emit(f = $stdout) ⇒ Object

output results to stdout or any IO



106
107
108
# File 'lib/temill/core.rb', line 106

def emit(f=$stdout)
  execute_emitter(Emitters::StdoutEmitter.new(f, @options))
end

#emit_to_directory(dir) ⇒ Object

output results to files in a directory. a file of original source code corresponds to a output file.



113
114
115
# File 'lib/temill/core.rb', line 113

def emit_to_directory(dir)
  execute_emitter(Emitters::DirectoryEmitter.new(dir, @options))
end

#emit_to_stringHash<String,String>

output results to strings.



119
120
121
# File 'lib/temill/core.rb', line 119

def emit_to_string
  execute_emitter(Emitters::StringEmitter.new(@options))
end

#eval(src, bind = TOPLEVEL_BINDING, fname = default_eval_fname(caller_locations.first)) ⇒ Object

same as Kernel.eval, but the evaluated code is handled as if it was an independent file.



94
95
96
97
98
# File 'lib/temill/core.rb', line 94

def eval(src, bind=TOPLEVEL_BINDING, fname=default_eval_fname(caller_locations.first))
  path = EVAL_PATH_PREFIX + Digest::SHA256.hexdigest(src) + '/' + fname
  @source_files[path] ||= SourceFile.from_inline_source(src, path, @options)
  Kernel.eval(src, bind, path)
end

#execute_emitter(emitter = nil, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/temill/core.rb', line 124

def execute_emitter(emitter=nil, &block)
  if emitter
    emitter.call(@source_files.values, &block)
  elsif block
    block.call(@source_files.values)
  else
    raise ArgumentError, 'no emitter specified'
  end
end

#resetObject

clear all results added by #show



41
42
43
44
45
# File 'lib/temill/core.rb', line 41

def reset
  # absolute_path => SourceFile
  @source_files = {}
  self
end

#set_options(**options) ⇒ Object

set options

See Also:

  • Temil#initialize


49
50
51
52
# File 'lib/temill/core.rb', line 49

def set_options(**options)
  @options.update(options)
  self
end

#show(val) ⇒ Object #show(*vals) ⇒ Object #show(&block) ⇒ Object

store values to be shown. You have to use emit to actually output the results.

Overloads:

  • #show(&block) ⇒ Object

    show the result of block



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/temill/core.rb', line 62

def show(*vals, &block)
  if not vals.empty? and block
    raise ArgumentError, 'either value or block can be specified'
  end

  loc = caller_locations.first
  path = loc.absolute_path

  sf = (@source_files[path] ||= SourceFile.from_path(path, @options))

  if block
    obj = block.call
  else
    case vals.size
    when 0
      obj = nil
    when 1
      obj = vals.first
    else
      obj = vals
    end
  end
  sf.add_result(loc, obj, block_given?)

  obj
end