Class: CML::Tag
- Inherits:
-
Object
show all
- Defined in:
- lib/cml/tag.rb
Direct Known Subclasses
CML::Tags::Checkbox, CML::Tags::Checkboxes, CML::Tags::Group, CML::Tags::Hidden, CML::Tags::Meta, CML::Tags::Option, CML::Tags::Radio, CML::Tags::Radios, CML::Tags::Ratings, CML::Tags::Select, CML::Tags::Text, CML::Tags::Textarea, CML::Tags::Unknown
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cml, opts = {}) ⇒ Tag
Returns a new instance of Tag.
5
6
7
8
9
10
|
# File 'lib/cml/tag.rb', line 5
def initialize(cml, opts = {})
@cml = cml.is_a?(String) ? Parser.parse(cml).at("/root/*") : cml
@attrs = @cml.attributes @tag = @cml.name
@opts = opts
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
3
4
5
|
# File 'lib/cml/tag.rb', line 3
def attrs
@attrs
end
|
#cml ⇒ Object
Returns the value of attribute cml.
3
4
5
|
# File 'lib/cml/tag.rb', line 3
def cml
@cml
end
|
#data ⇒ Object
Returns the value of attribute data.
3
4
5
|
# File 'lib/cml/tag.rb', line 3
def data
@data
end
|
#tag ⇒ Object
Returns the value of attribute tag.
3
4
5
|
# File 'lib/cml/tag.rb', line 3
def tag
@tag
end
|
Instance Method Details
#children ⇒ Object
60
61
62
|
# File 'lib/cml/tag.rb', line 60
def children
false
end
|
#classes ⇒ Object
64
65
66
67
|
# File 'lib/cml/tag.rb', line 64
def classes
@classes ||= [name] << validations << @attrs["class"].to_s.split(/ /)
@classes.uniq.reject {|c| c.length == 0 }.join(" ").strip
end
|
#convert(opts = nil) ⇒ Object
69
70
71
72
73
|
# File 'lib/cml/tag.rb', line 69
def convert(opts = nil)
@opts = opts if opts
html = to_html
html.strip.length == 0 ? Nokogiri::XML::Text.new(html, @cml.document) : Nokogiri::HTML.fragment(html)
end
|
#gold=(opts = {}) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/cml/tag.rb', line 20
def gold=(opts = {})
cur = gold?
advanced = ["strict", "regex"].any? {|k| opts && opts[k] }
if !cur
cur = advanced ? @cml.add_child(Parser.parse("<cml:gold/>").at("/root/*")) : @cml
end
if cur.name == "gold"
if !opts
cur.remove
elsif advanced
opts.each do |k,v|
cur[k] = v.to_s if v
end
else
cur.remove
cur = @cml
end
end
if cur.name != "gold"
if !opts
@cml.remove_attribute("gold")
elsif opts["src"] && opts["src"] != "#{name}_gold"
@cml["gold"] = opts["src"]
else
@cml["gold"] = "true"
end
end
end
|
#gold? ⇒ Boolean
16
17
18
|
# File 'lib/cml/tag.rb', line 16
def gold?
@attrs["gold"] ? @cml : @cml.at("./cml:gold")
end
|
#instructions ⇒ Object
105
106
107
108
|
# File 'lib/cml/tag.rb', line 105
def instructions
@instructions ||= @attrs["instructions"] || (@cml.at("./cml:instructions") ? @cml.at("./cml:instructions").inner_html : nil)
@instructions ? "<p class=\"instructions\">#{@instructions}</p>" : nil
end
|
#label(tag = "label") ⇒ Object
88
89
90
91
|
# File 'lib/cml/tag.rb', line 88
def label(tag = "label")
required = " <span class=\"required\">(required)</span>" if validations.include?("required")
"<#{tag} class=\"legend\">#{raw_label}#{required}</#{tag.split(" ")[0]}>" if raw_label.to_s != ""
end
|
#legend ⇒ Object
93
94
95
|
# File 'lib/cml/tag.rb', line 93
def legend
label("h2")
end
|
#name ⇒ Object
51
52
53
|
# File 'lib/cml/tag.rb', line 51
def name
(@attrs["name"] || @attrs["label"]).to_s.downcase.gsub(/\s/,"_").gsub(/\W/,"")
end
|
#prefix(name) ⇒ Object
12
13
14
|
# File 'lib/cml/tag.rb', line 12
def prefix(name)
@opts[:prefix] ? "#{@opts[:prefix]}[#{name}]" : "#{name}"
end
|
#raw_label ⇒ Object
83
84
85
86
|
# File 'lib/cml/tag.rb', line 83
def raw_label
label = @attrs["label"]
label ||= @attrs["name"].to_s.capitalize
end
|
#to_html ⇒ Object
75
76
77
|
# File 'lib/cml/tag.rb', line 75
def to_html
wrapper(Liquid::Template.parse(self.class::Template).render(data, [LiquidFilters]))
end
|
#to_liquid ⇒ Object
97
98
99
|
# File 'lib/cml/tag.rb', line 97
def to_liquid
to_html
end
|
#to_s ⇒ Object
79
80
81
|
# File 'lib/cml/tag.rb', line 79
def to_s
@cml.to_s
end
|
#validate? ⇒ Boolean
110
111
112
|
# File 'lib/cml/tag.rb', line 110
def validate?
true
end
|
#validations ⇒ Object
101
102
103
|
# File 'lib/cml/tag.rb', line 101
def validations
@validations ||= @attrs["validates"].to_s.split(/ /).map {|v| "validates-#{v}"}
end
|
#value ⇒ Object
124
125
126
|
# File 'lib/cml/tag.rb', line 124
def value
@attrs["value"].to_s
end
|
#wrapper(parsed, tag = "div") ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/cml/tag.rb', line 114
def wrapper(parsed, tag = "div")
if tag == "label" && instructions
tag = "div"
parsed = "<label>#{parsed}</label>"
end
"<#{tag} class=\"#{wrapper_classes}\">#{parsed}#{instructions}</#{tag}>"
end
|
#wrapper_classes ⇒ Object
55
56
57
58
|
# File 'lib/cml/tag.rb', line 55
def wrapper_classes
= @attrs["only-if"] ? "logic-only-if:#{@attrs["only-if"]}" : ""
"#{@tag} #{}".strip
end
|