Class: ErbPP

Inherits:
Object
  • Object
show all
Defined in:
ext/cumo/narray/gen/erbln.rb,
ext/cumo/narray/gen/erbpp2.rb

Defined Under Namespace

Classes: CountLnString, ERBLN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, erb_base = nil, **opts, &block) ⇒ ErbPP

Returns a new instance of ErbPP.



8
9
10
11
12
13
14
15
# File 'ext/cumo/narray/gen/erbpp2.rb', line 8

def initialize(parent=nil, erb_base=nil, **opts, &block)
  @parent = parent
  @children = []
  @opts = opts
  set erb_base: erb_base if erb_base
  @parent.add_child(self) if @parent
  instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_meth_id, *args, &block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'ext/cumo/narray/gen/erbpp2.rb', line 51

def method_missing(_meth_id, *args, &block)
  if args.empty?
    #$stderr.puts _meth_id.inspect
    v = get(_meth_id, *args, &block)
    return v if !v.nil?
  end
  method_missing_alias(_meth_id, *args, &block)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



17
18
19
# File 'ext/cumo/narray/gen/erbpp2.rb', line 17

def children
  @children
end

#parentObject

Returns the value of attribute parent.



18
19
20
# File 'ext/cumo/narray/gen/erbpp2.rb', line 18

def parent
  @parent
end

Instance Method Details

#add_child(child) ⇒ Object



20
21
22
# File 'ext/cumo/narray/gen/erbpp2.rb', line 20

def add_child(child)
  @children.push(child)
end

#descriptionObject Also known as: desc



41
42
43
44
45
# File 'ext/cumo/narray/gen/erbpp2.rb', line 41

def description
  if s = @opts[:description] || @opts[:desc]
    s.gsub(/\@\{/, "[").gsub(/\@\}/, "]")
  end
end

#find(name) ⇒ Object



117
118
119
# File 'ext/cumo/narray/gen/erbpp2.rb', line 117

def find(name)
  children.find { |x| x.name == name }
end

#find_tmpl(name) ⇒ Object



113
114
115
# File 'ext/cumo/narray/gen/erbpp2.rb', line 113

def find_tmpl(name)
  @parent.children.find { |x| x.name == name }
end

#get(key, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'ext/cumo/narray/gen/erbpp2.rb', line 28

def get(key, *args, &block)
  if respond_to?(key)
    return send(key, *args, &block)
  end
  if args.empty? && block.nil? && @opts.has_key?(key)
    return @opts[key]
  end
  if @parent
    return @parent.get(key, *args, &block)
  end
  nil
end

#init_defObject



110
111
# File 'ext/cumo/narray/gen/erbpp2.rb', line 110

def init_def
end

#load_erb(base_name) ⇒ Object

ERB Loader



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/cumo/narray/gen/erbpp2.rb', line 62

def load_erb(base_name)
  safe_level = nil
  trim_mode = '%<>'
  file = base_name + get(:erb_suffix)
  dirs = get(:erb_dir)
  dirs = [dirs] if !dirs.kind_of?(Array)
  dirs.each do |x|
    Dir.glob(x).each do |dir|
      path = File.join(dir, file)
      if File.exist?(path)
        if get(:line_number)
          erb = ERBLN.new(File.read(path), path, trim_mode)
        else
          erb = ERB.new(File.read(path), safe_level, trim_mode)
          erb.filename = path
        end
        return erb
      end
    end
  end
  nil
  # raise "file not found: #{file.inspect} in #{dirs.inspect}"
end

#method_missing_aliasObject



49
# File 'ext/cumo/narray/gen/erbpp2.rb', line 49

alias method_missing_alias method_missing

#resultObject



95
96
97
98
99
100
101
102
# File 'ext/cumo/narray/gen/erbpp2.rb', line 95

def result
  if base = @opts[:erb_base]
    # load_erb(base).result(binding)
    if l = load_erb(base)
      l.result(binding)
    end
  end
end

#runObject



86
87
88
89
90
91
92
93
# File 'ext/cumo/narray/gen/erbpp2.rb', line 86

def run
  if base = @opts[:erb_base]
    # load_erb(base).run(binding)
    if l = load_erb(base)
      l.run(binding)
    end
  end
end

#set(**opts) ⇒ Object



24
25
26
# File 'ext/cumo/narray/gen/erbpp2.rb', line 24

def set(**opts)
  @opts.merge!(opts)
end

#write(output) ⇒ Object



104
105
106
107
108
# File 'ext/cumo/narray/gen/erbpp2.rb', line 104

def write(output)
  File.open(output, "wt") do |f|
    f.print(result)
  end
end