Class: CodeReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/textutils/reader/code_reader.rb

Overview

fix: move into TextUtils namespace/module!!

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ CodeReader

Returns a new instance of CodeReader.



21
22
23
24
25
26
27
28
29
30
# File 'lib/textutils/reader/code_reader.rb', line 21

def initialize( arg )
  if arg.is_a?( String )  ## old style (deprecated) - pass in filepath as string
    path = arg
    logger.info "CodeReader.new - deprecated API - use CodeReader.from_file() instead"
    @code = File.read_utf8( path )
  else   ## assume it's a hash
    opts = arg
    @code = opts[:code]
  end
end

Class Method Details

.from_file(path) ⇒ Object



9
10
11
12
13
14
# File 'lib/textutils/reader/code_reader.rb', line 9

def self.from_file( path )
  ## nb: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  code = File.read_utf8( path )
  self.from_string( code )
end

.from_string(code) ⇒ Object



16
17
18
# File 'lib/textutils/reader/code_reader.rb', line 16

def self.from_string( code )
  CodeReader.new( code: code )
end

Instance Method Details

#eval(klass) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/textutils/reader/code_reader.rb', line 33

def eval( klass )
  klass.class_eval( @code )

  # NB: same as
  #
  # module WorldDB
  #   include WorldDB::Models
  #  <code here>
  # end
end