Class: Redwood::HorizontalSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/sup/horizontal_selector.rb

Defined Under Namespace

Classes: UnknownValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, vals, labels, base_color = :horizontal_selector_unselected_color, selected_color = :horizontal_selector_selected_color) ⇒ HorizontalSelector

Returns a new instance of HorizontalSelector.



8
9
10
11
12
13
14
15
16
# File 'lib/sup/horizontal_selector.rb', line 8

def initialize label, vals, labels, base_color=:horizontal_selector_unselected_color, selected_color=:horizontal_selector_selected_color
  @label = label
  @vals = vals
  @labels = labels
  @base_color = base_color
  @selected_color = selected_color
  @selection = 0
  @changed_by_user = false
end

Instance Attribute Details

#changed_by_userObject

Returns the value of attribute changed_by_user.



6
7
8
# File 'lib/sup/horizontal_selector.rb', line 6

def changed_by_user
  @changed_by_user
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/sup/horizontal_selector.rb', line 6

def label
  @label
end

Instance Method Details

#can_set_to?(val) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/sup/horizontal_selector.rb', line 23

def can_set_to? val
  @vals.include? val
end

#line(width = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sup/horizontal_selector.rb', line 29

def line width=nil
  label =
    if width
      sprintf "%#{width}s ", @label
    else
      "#{@label} "
    end

  [[@base_color, label]] +
    (0 ... @labels.length).inject([]) do |array, i|
      array + [
        if i == @selection
          [@selected_color, @labels[i]]
        else
          [@base_color, @labels[i]]
        end] + [[@base_color, "  "]]
    end + [[@base_color, ""]]
end

#roll_leftObject



48
49
50
51
# File 'lib/sup/horizontal_selector.rb', line 48

def roll_left
  @selection = (@selection - 1) % @labels.length
  @changed_by_user = true
end

#roll_rightObject



53
54
55
56
# File 'lib/sup/horizontal_selector.rb', line 53

def roll_right
  @selection = (@selection + 1) % @labels.length
  @changed_by_user = true
end

#set_to(val) ⇒ Object

Raises:



18
19
20
21
# File 'lib/sup/horizontal_selector.rb', line 18

def set_to val
  raise UnknownValue, val.inspect unless can_set_to? val
  @selection = @vals.index(val)
end

#valObject



27
# File 'lib/sup/horizontal_selector.rb', line 27

def val; @vals[@selection] end