Class: Charming::Focus
- Inherits:
-
Object
- Object
- Charming::Focus
- Defined in:
- lib/charming/focus.rb
Class Method Summary collapse
Instance Method Summary collapse
- #current ⇒ Object
- #cycle(direction = +1) ⇒ Object
- #define(slots) ⇒ Object
- #focus(slot) ⇒ Object
- #focused?(slot) ⇒ Boolean
-
#initialize(state) ⇒ Focus
constructor
A new instance of Focus.
- #pop_scope ⇒ Object
- #push_scope(slots, origin: :modal) ⇒ Object
- #ring ⇒ Object
Constructor Details
#initialize(state) ⇒ Focus
Returns a new instance of Focus.
12 13 14 |
# File 'lib/charming/focus.rb', line 12 def initialize(state) @state = state end |
Class Method Details
.for(session, controller_class) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/charming/focus.rb', line 5 def self.for(session, controller_class) session[:focus_state] ||= {} key = controller_class.name session[:focus_state][key] ||= {scopes: []} new(session[:focus_state][key]) end |
Instance Method Details
#current ⇒ Object
30 31 32 |
# File 'lib/charming/focus.rb', line 30 def current top && top[:current] end |
#cycle(direction = +1) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/charming/focus.rb', line 44 def cycle(direction = +1) return if ring.empty? index = ring.index(current) || 0 top[:current] = ring[(index + direction) % ring.length] end |
#define(slots) ⇒ Object
16 17 18 19 20 |
# File 'lib/charming/focus.rb', line 16 def define(slots) return if @state[:scopes].any? { |scope| scope[:origin] == :ring } @state[:scopes] << build_scope(slots, :ring) end |
#focus(slot) ⇒ Object
38 39 40 41 42 |
# File 'lib/charming/focus.rb', line 38 def focus(slot) return unless ring.include?(slot) top[:current] = slot end |
#focused?(slot) ⇒ Boolean
51 52 53 |
# File 'lib/charming/focus.rb', line 51 def focused?(slot) current == slot end |
#pop_scope ⇒ Object
26 27 28 |
# File 'lib/charming/focus.rb', line 26 def pop_scope @state[:scopes].pop end |
#push_scope(slots, origin: :modal) ⇒ Object
22 23 24 |
# File 'lib/charming/focus.rb', line 22 def push_scope(slots, origin: :modal) @state[:scopes] << build_scope(slots, origin) end |
#ring ⇒ Object
34 35 36 |
# File 'lib/charming/focus.rb', line 34 def ring top ? top[:ring] : [] end |