Module: Card::View::Options::Visibility

Included in:
Card::View::Options
Defined in:
lib/card/view/options/visibility.rb

Overview

manages showing and hiding optional view renders

Constant Summary collapse

VIZ_SETTING =

advanced write method

{ show: :show, true => :show,
hide: :hide, false => :hide, nil => :hide }.freeze

Instance Method Summary collapse

Instance Method Details

#explicit_show?(view) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/card/view/options/visibility.rb', line 22

def explicit_show? view
  viz_hash[view&.to_sym] == :show
end

#hide(*views) ⇒ Object



32
33
34
# File 'lib/card/view/options/visibility.rb', line 32

def hide *views
  viz views, :hide
end

#hide!(*views) ⇒ Object



42
43
44
# File 'lib/card/view/options/visibility.rb', line 42

def hide! *views
  viz views, :hide, true
end

#hide?(view) ⇒ Boolean

test methods

Returns:

  • (Boolean)


14
15
16
# File 'lib/card/view/options/visibility.rb', line 14

def hide? view
  viz_hash[view&.to_sym] == :hide
end

#optional?true/false

test whether view is optional (@optional is set in normalize_options

Returns:

  • (true/false)


67
68
69
# File 'lib/card/view/options/visibility.rb', line 67

def optional?
  @optional
end

#process_visibilityObject

translate raw hide, show options (which can be strings, symbols, arrays, etc)



73
74
75
76
77
# File 'lib/card/view/options/visibility.rb', line 73

def process_visibility
  viz_hash.reverse_merge! parent.viz_hash if parent
  process_visibility_options live_options
  viz requested_view, @optional if @optional && !viz_hash[requested_view]
end

#show(*views) ⇒ Object

write methods



28
29
30
# File 'lib/card/view/options/visibility.rb', line 28

def show *views
  viz views, :show
end

#show!(*views) ⇒ Object

force write methods



38
39
40
# File 'lib/card/view/options/visibility.rb', line 38

def show! *views
  viz views, :show, true
end

#show?(view) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/card/view/options/visibility.rb', line 18

def show? view
  !hide? view
end

#visible?(view) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/card/view/options/visibility.rb', line 59

def visible? view
  viz view, yield unless viz_hash[view]
  show? view
end

#viz(views, setting, force = false) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/card/view/options/visibility.rb', line 50

def viz views, setting, force=false
  Array.wrap(views).flatten.each do |view|
    view = view.to_sym
    next if !force && viz_hash[view]

    viz_hash[view] = VIZ_SETTING[setting]
  end
end

#viz_hashObject

tracks show/hide value for each view with an explicit setting eg { toggle: :hide }



8
9
10
# File 'lib/card/view/options/visibility.rb', line 8

def viz_hash
  @viz_hash ||= {}
end