Class: Sandals::View

Inherits:
Object
  • Object
show all
Defined in:
lib/sandals/view.rb

Constant Summary collapse

UNSET =
BindingResolver::UNSET

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(styles: {}) ⇒ View

Returns a new instance of View.



11
12
13
14
15
16
# File 'lib/sandals/view.rb', line 11

def initialize(styles: {})
  @children = []
  @containers = [self]
  @next_control_id = 0
  @styles = styles
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/sandals/view.rb', line 9

def children
  @children
end

Instance Method Details

#button(content, **styles, &action) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/sandals/view.rb', line 140

def button(content, **styles, &action)
  add Button.new(
    control_id("button"),
    content,
    action:,
    styles: styles_for(:button, styles)
  )
end

#checkbox(label, checked: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sandals/view.rb', line 92

def checkbox(
  label,
  checked: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  checked, error, action = resolve_binding(
    model:, bind:, value: checked, error:, action:, default: false
  )
  add Checkbox.new(control_id("checkbox"), label, checked:, error:, action:)
end

#em(content) ⇒ Object



161
162
163
# File 'lib/sandals/view.rb', line 161

def em(content)
  Emphasis.new(content)
end

#error(content) ⇒ Object



30
31
32
# File 'lib/sandals/view.rb', line 30

def error(content)
  add Error.new(content)
end

#file_field(label, error: nil, &action) ⇒ Object



136
137
138
# File 'lib/sandals/view.rb', line 136

def file_field(label, error: nil, &action)
  add FileField.new(control_id("file-field"), label, error:, action:)
end

#flow(**styles, &block) ⇒ Object



169
170
171
# File 'lib/sandals/view.rb', line 169

def flow(**styles, &block)
  container Flow.new(styles: styles_for(:flow, styles)), &block
end

#form(&block) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/sandals/view.rb', line 173

def form(&block)
  if @containers.any? { |container| container.is_a?(Form) }
    raise ArgumentError, "forms cannot be nested"
  end

  container Form.new, &block
end

#image(source, alt: "") ⇒ Object



38
39
40
# File 'lib/sandals/view.rb', line 38

def image(source, alt: "")
  add Image.new(source, alt:)
end


42
43
44
# File 'lib/sandals/view.rb', line 42

def link(content, to:)
  add Link.new(content, to:)
end

#notice(content) ⇒ Object



34
35
36
# File 'lib/sandals/view.rb', line 34

def notice(content)
  add Notice.new(content)
end

#number_field(label, value: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sandals/view.rb', line 64

def number_field(
  label,
  value: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  value, error, action = resolve_binding(
    model:, bind:, value:, error:, action:, default: nil
  )
  add NumberField.new(control_id("number-field"), label, value:, error:, action:)
end

#para(*parts, **styles) ⇒ Object



26
27
28
# File 'lib/sandals/view.rb', line 26

def para(*parts, **styles)
  add Para.new(parts, styles: styles_for(:para, styles))
end

#radio(label, options:, value: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sandals/view.rb', line 121

def radio(
  label,
  options:,
  value: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  value, error, action = resolve_binding(
    model:, bind:, value:, error:, action:, default: nil
  )
  add Radio.new(control_id("radio"), label, options:, value:, error:, action:)
end

#select(label, options:, value: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sandals/view.rb', line 106

def select(
  label,
  options:,
  value: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  value, error, action = resolve_binding(
    model:, bind:, value:, error:, action:, default: nil
  )
  add Select.new(control_id("select"), label, options:, value:, error:, action:)
end

#stack(**styles, &block) ⇒ Object



165
166
167
# File 'lib/sandals/view.rb', line 165

def stack(**styles, &block)
  container Stack.new(styles: styles_for(:stack, styles)), &block
end

#strong(content) ⇒ Object



157
158
159
# File 'lib/sandals/view.rb', line 157

def strong(content)
  Strong.new(content)
end

#submit(content, &action) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/sandals/view.rb', line 149

def submit(content, &action)
  unless @containers.any? { |container| container.is_a?(Form) }
    raise ArgumentError, "submit must be inside a form"
  end

  add Submit.new(control_id("submit"), content, action:)
end

#subtitle(content, **styles) ⇒ Object



22
23
24
# File 'lib/sandals/view.rb', line 22

def subtitle(content, **styles)
  add Subtitle.new(content, styles: styles_for(:subtitle, styles))
end

#table(headers:, rows:) ⇒ Object



46
47
48
# File 'lib/sandals/view.rb', line 46

def table(headers:, rows:)
  add Table.new(headers:, rows:)
end

#text_area(label, value: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sandals/view.rb', line 78

def text_area(
  label,
  value: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  value, error, action = resolve_binding(
    model:, bind:, value:, error:, action:, default: ""
  )
  add TextArea.new(control_id("text-area"), label, value:, error:, action:)
end

#text_field(label, value: UNSET, error: UNSET, model: UNSET, bind: UNSET, &action) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sandals/view.rb', line 50

def text_field(
  label,
  value: UNSET,
  error: UNSET,
  model: UNSET,
  bind: UNSET,
  &action
)
  value, error, action = resolve_binding(
    model:, bind:, value:, error:, action:, default: ""
  )
  add TextField.new(control_id("text-field"), label, value:, error:, action:)
end

#title(content, **styles) ⇒ Object



18
19
20
# File 'lib/sandals/view.rb', line 18

def title(content, **styles)
  add Title.new(content, styles: styles_for(:title, styles))
end