class Motion::Option

Attributes

actions[RW]

Public Class Methods

new() click to toggle source
# File lib/project/motion_option.rb, line 5
def initialize
  self.actions = []
  self
end

Public Instance Methods

[](index) click to toggle source
# File lib/project/motion_option.rb, line 14
def [](index)
  actions[index]
end
add(text, style=nil, &block) click to toggle source
# 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) click to toggle source
# 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
count() click to toggle source
# File lib/project/motion_option.rb, line 18
def count
  actions.count
end
each() click to toggle source
# File lib/project/motion_option.rb, line 26
def each
  actions.each
end
first() click to toggle source
# File lib/project/motion_option.rb, line 22
def first
  actions.first
end