Class: ErbPP

Inherits:
Object
  • Object
show all
Defined in:
lib/erbpp.rb,
ext/numo/narray/gen/erbpp2.rb

Defined Under Namespace

Classes: ParamNotSetError

Constant Summary collapse

ATTRS =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ErbPP.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/erbpp.rb', line 23

def initialize(parent,erb_path,opts={})
  parents.push(parent) if parent
  @erb_path = erb_path
  @tmpl = @erb_path

  @opts = opts
  if @opts.class != Hash
    raise ArgumentError, "option is not Hash"
  end

  @opts.each do |k,v|
    ivar = ("@"+k.to_s).to_sym
    instance_variable_set(ivar,v)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/erbpp.rb', line 83

def method_missing(_meth_id, *args, &block)
  ivar = "@"+_meth_id.to_s
  if args.empty? and instance_variable_defined?(ivar)
    parm = instance_variable_get(ivar)
    if parm.nil?
      raise ParamNotSetError,"parameter #{_meth_id.to_s} is not set"
    end
    parm
  elsif args.size == 1 and attrs.include?(_meth_id.to_s)
    instance_variable_set(ivar,args.first)
  elsif x = search_method_in_parents(_meth_id)
    x.send(_meth_id, *args, &block)
  else
    method_missing_alias(_meth_id, *args)
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'ext/numo/narray/gen/erbpp2.rb', line 14

def children
  @children
end

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'ext/numo/narray/gen/erbpp2.rb', line 15

def parent
  @parent
end

Class Method Details

.define_attrs(attrs) ⇒ Object



8
9
10
11
12
13
# File 'lib/erbpp.rb', line 8

def self.define_attrs(attrs)
  attrs.each do |attr|
    ivar = ("@"+attr).to_sym
    define_method(attr){|*a| attr_method(ivar,*a)}
  end
end

Instance Method Details

#add_child(child) ⇒ Object



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

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

#attr_method(ivar, arg = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/erbpp.rb', line 15

def attr_method(ivar,arg=nil)
  if arg.nil?
    instance_variable_get(ivar)
  else
    instance_variable_set(ivar,arg)
  end
end

#attrsObject



64
65
66
# File 'lib/erbpp.rb', line 64

def attrs
  self.class::ATTRS
end

#check_params(*params) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/erbpp.rb', line 72

def check_params(*params)
  params.each do |x|
    val = send(x)
    if !val # || val.empty?
      raise ParamNotSetError,"parameter #{x.to_s} is not set"
    end
  end
end

#descriptionObject Also known as: desc



38
39
40
41
42
# File 'ext/numo/narray/gen/erbpp2.rb', line 38

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

#find(name) ⇒ Object



103
104
105
# File 'ext/numo/narray/gen/erbpp2.rb', line 103

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

#find_tmpl(name) ⇒ Object



99
100
101
# File 'ext/numo/narray/gen/erbpp2.rb', line 99

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

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



25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/numo/narray/gen/erbpp2.rb', line 25

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

#has_attr?(_meth_id) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/erbpp.rb', line 68

def has_attr?(_meth_id)
  respond_to?(_meth_id) or attrs.include?(_meth_id.to_s)
end

#init_defObject



96
97
# File 'ext/numo/narray/gen/erbpp2.rb', line 96

def init_def
end

#load_erb(base_name) ⇒ Object

ERB Loader



59
60
61
62
63
64
# File 'ext/numo/narray/gen/erbpp2.rb', line 59

def load_erb
  safe_level = nil
  trim_mode = '%<>'
  @erb = ERB.new(File.read(@erb_path),safe_level,trim_mode)
  @erb.filename = @erb_path
end

#method_missing_aliasObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/erbpp.rb', line 81

def method_missing(_meth_id, *args, &block)
  ivar = "@"+_meth_id.to_s
  if args.empty? and instance_variable_defined?(ivar)
    parm = instance_variable_get(ivar)
    if parm.nil?
      raise ParamNotSetError,"parameter #{_meth_id.to_s} is not set"
    end
    parm
  elsif args.size == 1 and attrs.include?(_meth_id.to_s)
    instance_variable_set(ivar,args.first)
  elsif x = search_method_in_parents(_meth_id)
    x.send(_meth_id, *args, &block)
  else
    method_missing_alias(_meth_id, *args)
  end
end

#parentsObject



46
47
48
# File 'lib/erbpp.rb', line 46

def parents
  @parents ||= []
end

#resultObject



105
106
107
108
# File 'lib/erbpp.rb', line 105

def result
  load_erb unless @erb
  @erb.result(binding)
end

#runObject



100
101
102
103
# File 'lib/erbpp.rb', line 100

def run
  load_erb unless @erb
  @erb.run(binding)
end

#search_method_in_parents(_meth_id) ⇒ Object



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

def search_method_in_parents(_meth_id)
  parents.each do |x|
    if x.has_attr? _meth_id
      return x
    end
  end
  parents.each do |x|
    if f = x.search_method_in_parents(_meth_id)
      return f
    end
  end
  nil
end

#set(**opts) ⇒ Object



21
22
23
# File 'ext/numo/narray/gen/erbpp2.rb', line 21

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

#write(output) ⇒ Object



90
91
92
93
94
# File 'ext/numo/narray/gen/erbpp2.rb', line 90

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