Class: Fretboards::FretboardCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/fretboards/fretboard_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ FretboardCollection

Returns a new instance of FretboardCollection.



7
8
9
10
11
12
# File 'lib/fretboards/fretboard_collection.rb', line 7

def initialize(settings = {})
  @col = []
  @forms = {}
  @tuning = %w{ g' c' e' a' }
  @table = "default"
end

Instance Method Details

#add(dots, attrs = {}) ⇒ Object



18
19
20
21
22
# File 'lib/fretboards/fretboard_collection.rb', line 18

def add(dots, attrs = {})
  fb = fretboard(dots, attrs)
  @col << fb
  fb
end

#define(title, a, attrs = {}) ⇒ Object



24
25
26
# File 'lib/fretboards/fretboard_collection.rb', line 24

def define(title, a, attrs = {})
  form_add(title, a, { :title => title }.merge(attrs))
end

#form_add(name, a, attrs = {}) ⇒ Object



51
52
53
# File 'lib/fretboards/fretboard_collection.rb', line 51

def form_add(name, a, attrs = {})
  add(form_create(name, a, attrs))
end

#form_create(name, a, attrs = {}) ⇒ Object



55
56
57
# File 'lib/fretboards/fretboard_collection.rb', line 55

def form_create(name, a, attrs = {})
  @forms[name] = fretboard(a, attrs)
end

#fretboard(dots, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fretboards/fretboard_collection.rb', line 34

def fretboard(dots, opts = {})
  if opts.is_a? String
    attrs = {}
    attrs[:title] = opts
  else
    attrs = opts.dup
  end
  if dots.is_a? Fretboard
    fb = dots
    fb.title = attrs[:title] if attrs[:title]
  else
    fb = Fretboard.new(:tuning => @tuning)
    fb.terse(dots, attrs)
  end
  fb
end

#render_to_files(output_dir = '.', settings = {}) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/fretboards/fretboard_collection.rb', line 64

def render_to_files(output_dir = '.', settings = {})
  # TODO may we use a filenaming lambda?
  r = renderer(settings)
  @col.each do |fb|
    File.open("#{output_dir}/#{fb.title.gsub(/[^A-z0-9]/, "_")}.svg", "w") { |f| f.puts(r.render(fb)) }
  end
end

#renderer(settings = {}) ⇒ Object



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

def renderer(settings = {})
  Renderer::Svg.new(settings)
end

#set_tuning(a) ⇒ Object



14
15
16
# File 'lib/fretboards/fretboard_collection.rb', line 14

def set_tuning(a)
  @tuning = a
end

#use(title) ⇒ Object



28
29
30
31
# File 'lib/fretboards/fretboard_collection.rb', line 28

def use(title)
  raise "#{title} form not available" unless @forms[title]
  @forms[title] 
end