Class: CML::Tag
Direct Known Subclasses
CML::Tags::Checkbox, CML::Tags::Checkboxes, CML::Tags::Group, CML::Tags::Hidden, CML::Tags::Hours, CML::Tags::Iterate, CML::Tags::Meta, CML::Tags::Option, CML::Tags::Radio, CML::Tags::Radios, CML::Tags::Ratings, CML::Tags::Search, CML::Tags::Select, CML::Tags::Taxonomy, CML::Tags::Text, CML::Tags::Textarea, CML::Tags::Thumb, CML::Tags::Unknown
Constant Summary
Constants included
from TagLogic
CML::TagLogic::And, CML::TagLogic::AndPhraseExp, CML::TagLogic::CombinatorDefault, CML::TagLogic::CombinatorDict, CML::TagLogic::CombinatorExp, CML::TagLogic::GroupExp, CML::TagLogic::Or, CML::TagLogic::OrCombinatorExp, CML::TagLogic::PrecedenceRegexp, CML::TagLogic::TokenExp, CML::TagLogic::TokenRegexp
Instance Attribute Summary collapse
Attributes included from TagLogic
#errors, #has_grouped_logic, #has_liquid_logic, #logic_tree
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from TagLogic
#dependencies_on_fields, #dependencies_through_cml_group, #depends_on_fields, #describe_logic_token, #detect_grouped_logic, #detect_liquid_logic, #each_logic_token_in, #expand_logic, #expand_parsed_expression, #in_logic_graph?, #keep_merge!, #only_if, parse_expression, resolve_combinator
Constructor Details
#initialize(cml, opts = {}) ⇒ Tag
Returns a new instance of Tag.
27
28
29
30
31
32
|
# File 'lib/cml/tag.rb', line 27
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.
8
9
10
|
# File 'lib/cml/tag.rb', line 8
def attrs
@attrs
end
|
#cml ⇒ Object
Returns the value of attribute cml.
8
9
10
|
# File 'lib/cml/tag.rb', line 8
def cml
@cml
end
|
#data ⇒ Object
Returns the value of attribute data.
8
9
10
|
# File 'lib/cml/tag.rb', line 8
def data
@data
end
|
#opts ⇒ Object
Returns the value of attribute opts.
8
9
10
|
# File 'lib/cml/tag.rb', line 8
def opts
@opts
end
|
#tag ⇒ Object
Returns the value of attribute tag.
8
9
10
|
# File 'lib/cml/tag.rb', line 8
def tag
@tag
end
|
Class Method Details
.memoize(*methods) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/cml/tag.rb', line 12
def self.memoize(*methods)
methods.each do |m|
class_eval %Q{
alias :original_#{m} :#{m}
def #{m}(*args)
@#{m}_cache ||= {}
key = {:prefix => @opts[:prefix], :missed => @opts[:missed]}.merge(:_args => args)
return @#{m}_cache[key] if @#{m}_cache[key]
@#{m}_cache[key] = self.original_#{m}(*args)
end
}
end
end
|
Instance Method Details
#cacheable? ⇒ Boolean
235
236
237
|
# File 'lib/cml/tag.rb', line 235
def cacheable?
!(@opts[:normalize] || @opts[:show_data] || @opts[:no_memoize] || @opts[:missed])
end
|
#children ⇒ Object
153
154
155
|
# File 'lib/cml/tag.rb', line 153
def children
false
end
|
#classes ⇒ Object
157
158
159
160
161
|
# File 'lib/cml/tag.rb', line 157
def classes
@classes ||= [name] << validations << @attrs["class"].to_s.split(/ /)
@classes << "cml_data_preloaded" if @opts[:show_data] && @opts[:data][name]
@classes.uniq.reject {|c| c.length == 0 }.join(" ").strip
end
|
#convert(opts = nil) ⇒ Object
163
164
165
166
167
168
|
# File 'lib/cml/tag.rb', line 163
def convert(opts = nil)
@opts = @opts.merge(opts) if opts
html = to_html
html.strip.length == 0 ? Nokogiri::XML::Text.new(html, @cml.document) : Nokogiri::HTML.fragment(html)
end
|
#ensure_resolved(value) ⇒ Object
Check if the provided value contains a liquid variable If have a variable, data, and are not cachable return the resolved data
241
242
243
244
245
246
|
# File 'lib/cml/tag.rb', line 241
def ensure_resolved(value)
variable = value[/\{\{\s*([\w-]+)/, 1]
res = variable && @opts[:data] && !cacheable? ? @opts[:data][variable] : value
res.is_a?(Array) ? value : res
end
|
#finite_value? ⇒ Boolean
296
297
298
|
# File 'lib/cml/tag.rb', line 296
def finite_value?
(@attrs['is-finite'] && @attrs['is-finite'].value=='true') || false
end
|
#gold=(opts = {}) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/cml/tag.rb', line 112
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
104
105
106
|
# File 'lib/cml/tag.rb', line 104
def gold?
@attrs["gold"] ? @cml : @cml.at("./cml:gold")
end
|
#gold_class ⇒ Object
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# File 'lib/cml/tag.rb', line 270
def gold_class
if @opts[:missed]
if @opts[:missed][name]
bad = Array(@opts[:missed][name][0])
good = Array(@opts[:missed][name][1])
if bad.include?(value)
return "gold_bad"
elsif good.include?(value)
return "gold_good"
end
end
end
""
end
|
#gold_reason ⇒ Object
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/cml/tag.rb', line 207
def gold_reason
return "" unless @opts[:normalize] && !name.empty? && @opts[:no_reason].nil? && !["iterate", "group"].include?(@tag)
reason_name = @opts[:iteration] ? prefix(name+"_reason").sub(/\[\{\{i\}\}\]/,"") : prefix(name+"_reason").sub(/\[\]/,"")
<<-HTML
<div class="cml_gold_reason">
<label class="legend">Reason</label>
<textarea name="#{reason_name}">#{Array((@opts[:data] || {})[name+"_reason"])[@opts[:iteration].to_i]}</textarea>
</div>
HTML
end
|
#instructions ⇒ Object
226
227
228
229
|
# File 'lib/cml/tag.rb', line 226
def instructions
@instructions ||= @attrs["instructions"] || (@cml.at("./cml:instructions") ? @cml.at("./cml:instructions").inner_html : nil)
@instructions ? "<p class=\"instructions\">#{@instructions}</p>\n#{gold_reason}" : gold_reason
end
|
#iterating? ⇒ Boolean
108
109
110
|
# File 'lib/cml/tag.rb', line 108
def iterating?
@cml.at_xpath("ancestor::cml:iterate")
end
|
#label(tag = "label") ⇒ Object
191
192
193
194
|
# File 'lib/cml/tag.rb', line 191
def label(tag = "label")
required = " <span class=\"required\">(required)</span>" if validations.include?("required")
raw_label == "" ? "" : "<#{tag} class=\"legend\">#{raw_label}#{required}</#{tag.split(" ")[0]}>"
end
|
#legend ⇒ Object
196
197
198
|
# File 'lib/cml/tag.rb', line 196
def legend
label("h2")
end
|
#multi_type ⇒ Object
:column means a new column in the result csv, does this by adding _1 to the name :list means an array of values in the result csv :columnlist means a new column that is a list
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/cml/tag.rb', line 77
def multi_type
grouped = self.parent_multiples
if @attrs["multiple"] || !grouped.empty?
if @cml.at(".//cml:checkboxes|.//cml:radios|.//cml:ratings")
:column
elsif !grouped.empty?
if grouped.length > 1
:columnlist
elsif grouped.detect {|g| g.at(".//cml:checkboxes|.//cml:radios|.//cml:ratings") }
@attrs["multiple"] ? :columnlist : :column
else
:list
end
else
:list
end
else
nil
end
end
|
#name(auto_gold = true, suffix = "") ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/cml/tag.rb', line 57
def name(auto_gold = true, suffix="")
name = (@attrs["name"] || @attrs["label"]).to_s.downcase.gsub(/\s/,"_").gsub(/\W/,"")
name += ("_" + suffix) unless suffix.size == 0
if auto_gold && @opts[:normalize]
if g = @opts[:parser].golds[name]
name = g
else
name = "#{name}_gold" unless name =~ /_gold$/ || name.empty?
end
end
name = name.sub(/_gold$/,"") unless auto_gold
name + (multi_type.to_s =~ /column/ && !(name =~ /_1$/) ? "_1" : "")
end
|
#parent_multiples ⇒ Object
99
100
101
|
# File 'lib/cml/tag.rb', line 99
def parent_multiples
@cml.xpath("ancestor::cml:*[@multiple]")
end
|
#prefix(name) ⇒ Object
This is convoluted as fuck
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/cml/tag.rb', line 35
def prefix(name)
if @opts[:iteration]
m = case @tag
when "checkbox"
"[{{i}}][]"
when "radio"
"[{{i}}]"
when "rating"
@opts[:normalize] ? "[{{i}}][]" : "[{{i}}]"
when "text", "textarea"
@opts[:normalize] ? "[{{i}}][]" : "[]"
else
"[]"
end
elsif multi_type.to_s =~ /list$/
m = "[]"
else
m = @opts[:normalize] ? "[]" : ""
end
@opts[:prefix] ? "#{@opts[:prefix]}[#{name}]#{m}" : name
end
|
#preloaded_data ⇒ Object
266
267
268
|
# File 'lib/cml/tag.rb', line 266
def preloaded_data
@opts[:show_data] && @opts[:data][name]
end
|
#raw_label ⇒ Object
185
186
187
188
189
|
# File 'lib/cml/tag.rb', line 185
def raw_label
label = @attrs["label"] ? @attrs["label"].to_s : nil
label = cml.inner_text if label.nil? && @tag == "option"
label ||= (@attrs["name"] || "").to_s.capitalize
end
|
#to_html ⇒ Object
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/cml/tag.rb', line 170
def to_html
template = @opts[:bare] ? self.class::BareTemplate : self.class::Template
wrapper(Liquid::Template.parse(template).render(data, [LiquidFilters]))
end
|
#to_liquid ⇒ Object
200
201
202
203
204
205
|
# File 'lib/cml/tag.rb', line 200
def to_liquid
LiquidHash[
"to_s" => to_html,
"raw_label" => raw_label.to_s
]
end
|
#to_s ⇒ Object
181
182
183
|
# File 'lib/cml/tag.rb', line 181
def to_s
@cml.to_s
end
|
#validate? ⇒ Boolean
231
232
233
|
# File 'lib/cml/tag.rb', line 231
def validate?
true
end
|
#validations ⇒ Object
222
223
224
|
# File 'lib/cml/tag.rb', line 222
def validations
@validations ||= validators.map {|v| v.to_s.empty? ? nil : "validates-#{v}"}.compact
end
|
#validators ⇒ Object
218
219
220
|
# File 'lib/cml/tag.rb', line 218
def validators
@validators ||= @attrs["validates"].to_s.split(/ /)
end
|
#value ⇒ Object
262
263
264
|
# File 'lib/cml/tag.rb', line 262
def value
ensure_resolved(@attrs["value"].to_s)
end
|
#wrapper(parsed, tag = "div") ⇒ Object
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/cml/tag.rb', line 248
def wrapper(parsed, tag = "div")
if tag == "label" && instructions
tag = "div"
parsed = "<label>#{parsed}</label>"
end
style = @attrs["style"] ? "style=\"#{@attrs["style"]}\"" : ""
data_attrs = data_attributes_string || ""
"<#{tag} class=\"#{wrapper_classes}\" #{style} #{data_attrs}>#{parsed}#{instructions}</#{tag}>"
end
|
#wrapper_classes ⇒ Object
143
144
145
146
147
148
149
150
151
|
# File 'lib/cml/tag.rb', line 143
def wrapper_classes
= []
<< "logic-only-if:#{@attrs["only-if"]}" if @attrs["only-if"]
<< "matcher:#{@attrs["matcher"]}" if @attrs["matcher"]
+= ["multiple",multi_type] if @attrs["multiple"]
<< "cml_field" unless ["group","iterate"].include?(@tag)
<< @attrs["class"].to_s if @attrs["class"]
"#{@tag.gsub(/_/," ")} #{.join(" ")}".strip
end
|