Class: Plotrobber::PlotWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/plotrobber.rb,
lib/plotrobber.rb

Constant Summary collapse

QUOTED =
%w[title output xlabel ylabel]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlotWrapper

Returns a new instance of PlotWrapper.



47
48
49
50
# File 'lib/plotrobber.rb', line 47

def initialize
  @commands  = []
  @functions = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



45
46
47
# File 'lib/plotrobber.rb', line 45

def commands
  @commands
end

#functionsObject

Returns the value of attribute functions.



45
46
47
# File 'lib/plotrobber.rb', line 45

def functions
  @functions
end

Class Method Details

.before_eachObject



42
# File 'lib/plotrobber.rb', line 42

def self.before_each ; @before_each ; end

.before_each=(blk) ⇒ Object



43
# File 'lib/plotrobber.rb', line 43

def self.before_each=(blk) ; @before_each = blk ; end

Instance Method Details

#candlesticks(name, extra_opts = {}) ⇒ Object



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

def candlesticks(name, extra_opts = {})
  with_opt = case extra_opts[:with]
    when Hash, Array then 'candlesticks ' + extra_opts[:with].to_a.join(' ')
    when String      then 'candlesticks ' + extra_opts[:with]
    else                  'candlesticks'
  end

  options = {using: '1:3:2:6:5', with: with_opt}.merge(extra_opts)

  show name, options
end

#fit(what, using, use_b = true) ⇒ Object



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

def fit(what, using, use_b = true)
  name = source_name what

  @next_fn ||= 0
  @next_fn += 1

  f, m, b = %w[f m b].map {|x| "#{x}#{@next_fn}" }
  using_str = using.join(':')
  via_str = use_b ? "#{m}, #{b}" : m

  please '%s(x) = %s*x + %s + 1e-9' % [f, m, use_b ? b : 0]
  please 'fit %s(x) %s using %s via %s' % [f, name, using_str, via_str]

  "#{f}(x)"
end

#magic_terminal(name) ⇒ Object



94
95
96
97
98
99
# File 'lib/plotrobber.rb', line 94

def magic_terminal(name)
  unless ENV.key? 'STAGE'
    terminal File.extname(name)[1..-1]
    output name
  end
end

#please(s) ⇒ Object



56
57
58
# File 'lib/plotrobber.rb', line 56

def please(s)
  @commands << s
end

#quote(k, v) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/plotrobber.rb', line 65

def quote(k, v)
  if QUOTED.include?(k.to_s) and v !~ /\A'.*'\z/
    '"%s"' % v
  else
    v
  end
end

#set(s) ⇒ Object



60
61
62
# File 'lib/plotrobber.rb', line 60

def set(s)
  please "set #{s}"
end

#show(what, *args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/plotrobber.rb', line 145

def show(what, *args)
  name = source_name what

  output = [name]

  for arg in args
    if Hash === arg
      for k, v in arg
        output << k
        output << quote(k, v) unless v === true
      end
    else
      output << arg.to_s
    end
  end

  @functions << output.join(' ')
end

#source_name(what) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/plotrobber.rb', line 101

def source_name(what)
  name = case what
    when String         then what
    when Array          then store(what).path
    when Tempfile, File then what.path
    else                     what.to_s
  end

  if !name.start_with?("'") and File.exists?(name)
    name = "'#{name}'"
  end
  
  name
end

#store(data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/plotrobber.rb', line 83

def store(data)
  (@store ||= {})[data] ||= begin
    file = Tempfile.new(['plotrobber.','.dat'])
    file.write Array(data).map{|x|Array(x).join("\t")}.join("\n")
    file.write "\n"
    file.rewind

    file
  end
end

#to_sObject



52
53
54
# File 'lib/plotrobber.rb', line 52

def to_s
  @commands.map(&:to_s).join("\n") + "\nplot\\\n\t" + @functions.join(",\\\n\t")
end