Class: Motion::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/project/motion_option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



5
6
7
8
# File 'lib/project/motion_option.rb', line 5

def initialize
  self.actions = []
  self
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



3
4
5
# File 'lib/project/motion_option.rb', line 3

def actions
  @actions
end

Instance Method Details

#[](index) ⇒ Object



14
15
16
# File 'lib/project/motion_option.rb', line 14

def [](index)
  actions[index]
end

#add(text, style = nil, &block) ⇒ Object



10
11
12
# File 'lib/project/motion_option.rb', line 10

def add(text, style=nil, &block)
  actions << Action.new(title: text, style: style, action: block)
end

#attach_to(view) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/project/motion_option.rb', line 30

def attach_to(view)
  case view
  when UIAlertView, UIActionSheet
    actions.each_with_index do |action, index|
      view.addButtonWithTitle(action.title)
      case action.style
      when UIAlertActionStyleCancel
        view.cancelButtonIndex = index
      when UIAlertActionStyleDestructive
        view.destructiveButtonIndex = index
      end
    end
  when UIAlertController
    actions.each do |a|
      alert_action = UIAlertAction.actionWithTitle(
        a.title,
        style: a.style,
        handler: ->(arg) { a.action ? a.action.call : nil }
      )
      view.addAction(alert_action)
    end
  end

  view
end

#countObject



18
19
20
# File 'lib/project/motion_option.rb', line 18

def count
  actions.count
end

#eachObject



26
27
28
# File 'lib/project/motion_option.rb', line 26

def each
  actions.each
end

#firstObject



22
23
24
# File 'lib/project/motion_option.rb', line 22

def first
  actions.first
end