Module: Card::View::Visibility

Included in:
Card::View
Defined in:
lib/card/view/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 }

Instance Method Summary collapse

Instance Method Details

#hide(*views) ⇒ Object



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

def hide *views
  viz views, :hide
end

#hide!(*views) ⇒ Object



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

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

#hide?(view) ⇒ Boolean

test methods

Returns:

  • (Boolean)


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

def hide? view
  viz_hash[view] == :hide
end

#optional?Boolean

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

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/card/view/visibility.rb', line 62

def optional?
  return @optional unless @optional.nil?
  @optional = detect_if_optional
end

#process_visibility(options_hash) ⇒ Object

takes an options_hash and processes it to update viz_hash



76
77
78
79
80
81
# File 'lib/card/view/visibility.rb', line 76

def process_visibility options_hash
  [:hide, :show].each do |setting|
    list = viz_view_list options_hash.delete(setting)
    viz list, setting, true
  end
end

#process_visibility_optionsObject

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



69
70
71
72
73
# File 'lib/card/view/visibility.rb', line 69

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

#show(*views) ⇒ Object

write methods



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

def show *views
  viz views, :show
end

#show!(*views) ⇒ Object

force write methods



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

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

#show?(view) ⇒ Boolean

Returns:

  • (Boolean)


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

def show? view
  !hide? view
end

#visible?(view) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/card/view/visibility.rb', line 53

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

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



45
46
47
48
49
50
51
# File 'lib/card/view/visibility.rb', line 45

def viz views, setting, force=false
  Array.wrap(views).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 }



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

def viz_hash
  @viz_hash ||= {}
end

#viz_view_list(val) ⇒ Object

translated show/hide setting into an array of views



84
85
86
87
88
89
90
91
92
# File 'lib/card/view/visibility.rb', line 84

def viz_view_list val
  case val
  when NilClass then []
  when Array    then val
  when String   then val.split(/[\s,]+/)
  when Symbol   then [val]
  else raise Card::Error, "bad show/hide argument: #{val}"
  end.map { |view| View.canonicalize view }
end