Class: Oxidized::Model

Inherits:
Object
  • Object
show all
Includes:
Config::Vars
Defined in:
lib/oxidized/model/model.rb,
lib/oxidized/model/outputs.rb

Defined Under Namespace

Classes: Outputs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::Vars

#vars

Instance Attribute Details

#inputObject

Returns the value of attribute input.



78
79
80
# File 'lib/oxidized/model/model.rb', line 78

def input
  @input
end

#nodeObject

Returns the value of attribute node.



78
79
80
# File 'lib/oxidized/model/model.rb', line 78

def node
  @node
end

Class Method Details

.cfg(*methods, &block) ⇒ Object



23
24
25
26
27
# File 'lib/oxidized/model/model.rb', line 23

def cfg *methods, &block
  [methods].flatten.each do |method|
    @cfg[method.to_s] << block
  end
end

.cfgsObject



28
29
30
# File 'lib/oxidized/model/model.rb', line 28

def cfgs
  @cfg
end

.cmd(_cmd = nil, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/oxidized/model/model.rb', line 31

def cmd _cmd=nil, &block
  if _cmd.class == Symbol
    @cmd[_cmd] << block
  else
    @cmd[:cmd] << [_cmd, block]
  end
end

.cmdsObject



38
39
40
# File 'lib/oxidized/model/model.rb', line 38

def cmds
  @cmd
end

.comment(_comment = '# ') ⇒ Object



16
17
18
19
# File 'lib/oxidized/model/model.rb', line 16

def comment _comment='# '
  return @comment if @comment
  @comment = block_given? ? yield : _comment
end

.expect(re, &block) ⇒ Object



41
42
43
# File 'lib/oxidized/model/model.rb', line 41

def expect re, &block
  @expect << [re, block]
end

.expectsObject



44
45
46
# File 'lib/oxidized/model/model.rb', line 44

def expects
  @expect
end

.inherited(klass) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/oxidized/model/model.rb', line 9

def inherited klass
  klass.instance_variable_set '@cmd',   Hash.new { |h,k| h[k] = [] }
  klass.instance_variable_set '@cfg',   Hash.new { |h,k| h[k] = [] }
  klass.instance_variable_set '@procs', Hash.new { |h,k| h[k] = [] }
  klass.instance_variable_set '@expect', []
  klass.const_set :CFG, CFG
end

.post { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, adding the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



66
67
68
# File 'lib/oxidized/model/model.rb', line 66

def post &block
  @procs[:post] << block
end

.pre { ... } ⇒ void

This method returns an undefined value.

calls the block at the end of the model, prepending the output of the block to the output string

Yields:

  • expects block which should return [String]

Author:

Since:

  • 0.0.39



55
56
57
# File 'lib/oxidized/model/model.rb', line 55

def pre &block
  @procs[:pre] << block
end

.procsHash

Returns hash proc procs :pre+:post to be prepended/postfixed to output.

Returns:

  • (Hash)

    hash proc procs :pre+:post to be prepended/postfixed to output

Author:

Since:

  • 0.0.39



73
74
75
# File 'lib/oxidized/model/model.rb', line 73

def procs
  @procs
end

.prompt(_prompt = nil) ⇒ Object



20
21
22
# File 'lib/oxidized/model/model.rb', line 20

def prompt _prompt=nil
  @prompt or @prompt = _prompt
end

Instance Method Details

#cfgObject



107
108
109
# File 'lib/oxidized/model/model.rb', line 107

def cfg
  self.class.cfgs
end

#cmd(string, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/oxidized/model/model.rb', line 80

def cmd string, &block
  out = @input.cmd string
  return false unless out
  self.class.cmds[:all].each do |all_block|
    out = instance_exec Oxidized::String.new(out), string, &all_block
  end
  if vars :remove_secret
    self.class.cmds[:secret].each do |all_block|
      out = instance_exec Oxidized::String.new(out), string, &all_block
    end
  end
  out = instance_exec Oxidized::String.new(out), &block if block
  process_cmd_output out, string
end

#comment(_comment) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/oxidized/model/model.rb', line 145

def comment _comment
  data = ''
  _comment.each_line do |line|
    data << self.class.comment << line
  end
  data
end

#expect(re, &block) ⇒ Object



103
104
105
# File 'lib/oxidized/model/model.rb', line 103

def expect re, &block
  self.class.expect re, &block
end

#expects(data) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/oxidized/model/model.rb', line 115

def expects data
  self.class.expects.each do |re, cb|
    if data.match re
      if cb.arity == 2
        data = instance_exec [data, re], &cb
      else
        data = instance_exec data, &cb
      end
    end
  end
  data
end

#getObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/oxidized/model/model.rb', line 128

def get
  outputs = Outputs.new
  procs = self.class.procs
  self.class.cmds[:cmd].each do |command, block|
    out = cmd command, &block
    return false unless out
    outputs << out
  end
  procs[:pre].each do |pre_proc|
    outputs.unshift process_cmd_output(instance_eval(&pre_proc), '')
  end
  procs[:post].each do |post_proc|
    outputs << process_cmd_output(instance_eval(&post_proc), '')
  end
  outputs
end

#outputObject



95
96
97
# File 'lib/oxidized/model/model.rb', line 95

def output
  @input.output
end

#promptObject



111
112
113
# File 'lib/oxidized/model/model.rb', line 111

def prompt
  self.class.prompt
end

#send(data) ⇒ Object



99
100
101
# File 'lib/oxidized/model/model.rb', line 99

def send data
  @input.send data
end