Class: Z4app::Embed

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

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Embed

Returns a new instance of Embed.



17
18
19
20
# File 'lib/z4app.rb', line 17

def initialize script
  @o = []
  self.instance_eval script
end

Instance Method Details

#check(k) ⇒ Object

insert checkbox.



72
73
74
# File 'lib/z4app.rb', line 72

def check k
  @o << %[<h1 class='e'><input type="checkbox" id="e_#{k}" name="embed[#{k}]" value="true"><label for="e_#{k}">#{k}</label></h1>]
end

#color(k) ⇒ Object

insert color.



56
57
58
# File 'lib/z4app.rb', line 56

def color k
  @o << %[<h1 class='e'><input type='color' id='e_#{e}' name='embed[#{k}]'><label for='e_#{k}'>#{k}</label></h1>]
end

#date(k) ⇒ Object

insert date.



60
61
62
# File 'lib/z4app.rb', line 60

def date k
  @o << %[<h1 class='e'><input type='date' name='embed[#{k}]'></h1>]
end

#email(k) ⇒ Object

insert email field.



39
40
41
# File 'lib/z4app.rb', line 39

def email k
  @o << %[<h1 class='e'><input type='email' name='embed[#{k}]' placeholder='#{k}' pattern='.+@.+' title='valid email.'></h1>]
end

#goto(g, h = {}) ⇒ Object

set goto field.



91
92
93
94
# File 'lib/z4app.rb', line 91

def goto g, h={}
  a = []; h.each_pair { |k,v| a << %[#{k}=#{v}] }
  @o << %[<input type='hidden' name='goto' value='/#{g}?#{a.join("&")}'>]
end

#input(key, h = {}) ⇒ Object

insert text input.



34
35
36
37
# File 'lib/z4app.rb', line 34

def input key, h={}
  hh = { pattern: '.*', title: 'required.' }.merge(h)
  @o << %[<h1 class='e'><input name="embed[#{key}]" pattern='#{hh[:pattern]}' title='#{hh[:title]}' placeholder="#{key}"></h1>]
end

#number(k, h = {}) ⇒ Object

insert number field.



82
83
84
85
# File 'lib/z4app.rb', line 82

def number k, h={}
  hh = { min: 0, max: 100, value: 1 }.merge(h)
  @o << %[<h1 class='e'><input type="number" name="embed[#{k}]" value='#{hh[:value]}' min='#{hh[:min]}' max='#{hh[:max]}' placeholder='#{k}'></h1>]
end

#param(key) ⇒ Object

insert param into embed.



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

def param key
  return %[<%= params[:#{key}] %>]
end

#radio(k) ⇒ Object

insert radio button.



68
69
70
# File 'lib/z4app.rb', line 68

def radio k
  @o << %[<h1 class='e'><input type="radio" id="e_#{k}" name="embed[#{k}]" value="true"><label for="e_#{k}">#{k}</label></h1>]
end

#result(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/z4app.rb', line 21

def result params={}
  a = [
    %[<div id='embed'>],
    @o.join(""),
    %[</div>]
  ]
  return ERB.new(a.join("")).result(binding)
end

#select(k, *opts) ⇒ Object

insert selector.



76
77
78
79
80
# File 'lib/z4app.rb', line 76

def select k, *opts
  o = []
  [opts].flatten.each {|e| o << %[<option value="#{e}">#{e}</option>] }
  @o << %[<h1 class='e'><select name="embed[#{k}]">#{o.join("")}</select></h1>]
end

#stack(c, o) ⇒ Object

set stack push object.



100
101
102
103
# File 'lib/z4app.rb', line 100

def stack c, o
  @o << %[<input type='hidden' name='action' value='#{c}'>]
  @o << %[<input type='hidden' name='push' value='#{o}'>]
end

#submit(p) ⇒ Object

add submit button.



96
97
98
# File 'lib/z4app.rb', line 96

def submit p
  @o << %[<h1 class='e'><button id='send'>#{p}</button></h1>]
end

#tel(k) ⇒ Object

insert phone field.



47
48
49
# File 'lib/z4app.rb', line 47

def tel k
  @o << %[<h1 class='e'><input type='tel' name='embed[#{k}] placeholder='#{k}' pattern='\d{10}' title='valid phone number.'></h1>]
end

#text(t) ⇒ Object

insert plain text.



87
88
89
# File 'lib/z4app.rb', line 87

def text t
  @o << %[<h2 class='e'>#{t}</h2>]
end

#textarea(key, h = {}) ⇒ Object

insert textarea.



51
52
53
54
# File 'lib/z4app.rb', line 51

def textarea key, h={}
  hh = { placeholder: '', value: ''}.merge(h)
  @o << %[<textarea name='embed[#{key}]' placeholder='#{hh[:placeholder]}'>#{hh[:value]}</textarea>]
end

#time(k) ⇒ Object

insert time.



64
65
66
# File 'lib/z4app.rb', line 64

def time k
  @o << %[<h1 class='e'><input type='datetime-local' name='embed[#{k}]'></h1>]
end

#url(k) ⇒ Object

insert url field.



43
44
45
# File 'lib/z4app.rb', line 43

def url k
  @o << %[<h1 class='e'><input type='url' name='embed[#{k}] placeholder='#{k}' pattern='https://.*' title='valid url.'></h1>]
end