Class: CFML::Tag
- Inherits:
-
Object
show all
- Defined in:
- lib/cfml/tag.rb
Direct Known Subclasses
CFML::Tags::Checkbox, CFML::Tags::Checkboxes, CFML::Tags::Group, CFML::Tags::Hidden, CFML::Tags::Option, CFML::Tags::Radio, CFML::Tags::Radios, CFML::Tags::Ratings, CFML::Tags::Select, CFML::Tags::Text, CFML::Tags::Textarea, CFML::Tags::Unknown
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cfml, opts = {}) ⇒ Tag
Returns a new instance of Tag.
5
6
7
8
9
10
|
# File 'lib/cfml/tag.rb', line 5
def initialize(cfml, opts = {})
@cfml = cfml.is_a?(String) ? Parser.parse(cfml).root.children.first : cfml
@attrs = @cfml.attributes @tag = @cfml.name
@opts = opts
end
|
Instance Attribute Details
#attrs ⇒ Object
Returns the value of attribute attrs.
3
4
5
|
# File 'lib/cfml/tag.rb', line 3
def attrs
@attrs
end
|
#data ⇒ Object
Returns the value of attribute data.
3
4
5
|
# File 'lib/cfml/tag.rb', line 3
def data
@data
end
|
#tag ⇒ Object
Returns the value of attribute tag.
3
4
5
|
# File 'lib/cfml/tag.rb', line 3
def tag
@tag
end
|
Instance Method Details
#children ⇒ Object
29
30
31
|
# File 'lib/cfml/tag.rb', line 29
def children
false
end
|
#classes ⇒ Object
33
34
35
36
|
# File 'lib/cfml/tag.rb', line 33
def classes
@classes ||= [name] << validations << @attrs["class"].to_s.split(/ /)
@classes.uniq.reject {|c| c.length == 0 }.join(" ").strip
end
|
#convert ⇒ Object
38
39
40
|
# File 'lib/cfml/tag.rb', line 38
def convert
Nokogiri::HTML.fragment(to_html).children.first
end
|
#gold? ⇒ Boolean
16
17
18
|
# File 'lib/cfml/tag.rb', line 16
def gold?
@attrs["gold"] ? @cfml : @cfml.at("./cf:gold")
end
|
#instructions ⇒ Object
69
70
71
72
|
# File 'lib/cfml/tag.rb', line 69
def instructions
@instructions ||= @attrs["instructions"] || (@cfml.at("./cf:instructions") ? @cfml.at("./cf:instructions").inner_html : nil)
@instructions ? "<p class=\"instructions\">#{@instructions}</p>" : nil
end
|
#label(label, tag = "label") ⇒ Object
51
52
53
54
55
|
# File 'lib/cfml/tag.rb', line 51
def label(label, tag = "label")
label ||= @attrs["name"].to_s.capitalize
required = " <span class=\"required\">(required)</span>" if validations.include?("required")
"<#{tag}>#{label}#{required}</#{tag.split(" ")[0]}>" if label.to_s != ""
end
|
#legend(label) ⇒ Object
57
58
59
|
# File 'lib/cfml/tag.rb', line 57
def legend(label)
label(label, "legend") if label
end
|
#name ⇒ Object
20
21
22
|
# File 'lib/cfml/tag.rb', line 20
def name
(@attrs["name"] || @attrs["label"]).to_s.downcase.gsub(/\s/,"_").gsub(/\W/,"")
end
|
#prefix(name) ⇒ Object
12
13
14
|
# File 'lib/cfml/tag.rb', line 12
def prefix(name)
@opts[:prefix] ? "#{@opts[:prefix]}[#{name}]" : "#{name}"
end
|
#to_html ⇒ Object
42
43
44
45
|
# File 'lib/cfml/tag.rb', line 42
def to_html
@parsed ||= Liquid::Template.parse(self.class::Template).render(data, [LiquidFilters])
wrapper(@parsed)
end
|
#to_liquid ⇒ Object
61
62
63
|
# File 'lib/cfml/tag.rb', line 61
def to_liquid
to_html
end
|
#to_s ⇒ Object
47
48
49
|
# File 'lib/cfml/tag.rb', line 47
def to_s
@cfml.to_s
end
|
#validate? ⇒ Boolean
74
75
76
|
# File 'lib/cfml/tag.rb', line 74
def validate?
true
end
|
#validations ⇒ Object
65
66
67
|
# File 'lib/cfml/tag.rb', line 65
def validations
@validations ||= @attrs["validations"].to_s.split(/ /).map {|v| "validates-#{v}"}
end
|
#value ⇒ Object
88
89
90
|
# File 'lib/cfml/tag.rb', line 88
def value
@attrs["value"].to_s
end
|
#wrapper(parsed, tag = "div") ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/cfml/tag.rb', line 78
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
24
25
26
27
|
# File 'lib/cfml/tag.rb', line 24
def wrapper_classes
= @attrs["only-if"] ? "only-if:#{@attrs["only-if"]}" : ""
"#{@tag} #{}".strip
end
|