Class: Quby::Questionnaires::DSL::ChartBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/questionnaires/dsl/charting/chart_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(questionnaire, key, options = {}) ⇒ ChartBuilder

Returns a new instance of ChartBuilder.



19
20
21
22
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 19

def initialize(questionnaire, key, options = {})
  @chart = self.class.chart_class.new(key, options)
  @questionnaire = questionnaire
end

Class Method Details

.chart_classObject

rubocop:enable AccessorMethodName



15
16
17
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 15

def self.chart_class
  @chart_class
end

.set_chart_class(chart_class) ⇒ Object

rubocop:disable AccessorMethodName



10
11
12
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 10

def self.set_chart_class(chart_class)
  @chart_class = chart_class
end

Instance Method Details

#build(&block) ⇒ Object



58
59
60
61
62
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 58

def build(&block)
  instance_eval(&block)
  validate!
  @chart
end

#chart_type(type) ⇒ Object



54
55
56
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 54

def chart_type(type)
  @chart.chart_type = type
end

#plot(key, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 44

def plot(key, options = {})
  unless plottable = @questionnaire.find_plottable(key)
    fail "Questionnaire #{@questionnaire.key} chart #{@chart.key} references unknown score or question #{key}"
  end

  configure_options plottable, options

  @chart.plottables << Entities::Charting::Plottable.new(plottable.key, options)
end

#range(range) ⇒ Object



28
29
30
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 28

def range(range)
  @chart.y_range = range
end

#tick_interval(tick_interval) ⇒ Object



32
33
34
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 32

def tick_interval(tick_interval)
  @chart.tick_interval = tick_interval
end

#title(title) ⇒ Object



24
25
26
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 24

def title(title)
  @chart.title = title
end

#validate!Object



64
65
66
67
68
69
70
71
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 64

def validate!
  if @chart.y_categories.present? && @chart.y_range.present? &&
     @chart.y_range != (0..(@chart.y_categories.count - 1))
    fail ArgumentError, 'Y_categories size and range do not match'
  end

  true
end

#y_categories(y_categories) ⇒ Object



36
37
38
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 36

def y_categories(y_categories)
  @chart.y_categories = y_categories
end

#y_range_categories(*y_range_categories) ⇒ Object



40
41
42
# File 'lib/quby/questionnaires/dsl/charting/chart_builder.rb', line 40

def y_range_categories(*y_range_categories)
  @chart.y_range_categories = RangeCategories.new(*y_range_categories).as_range_hash
end