Class: CFML::Tag

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

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#Hash[*cfml.attributes.map {|k,v| [k.intern,v]}.flatten]
  @tag = @cfml.name
  @opts = opts
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



3
4
5
# File 'lib/cfml/tag.rb', line 3

def attrs
  @attrs
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/cfml/tag.rb', line 3

def data
  @data
end

#tagObject (readonly)

Returns the value of attribute tag.



3
4
5
# File 'lib/cfml/tag.rb', line 3

def tag
  @tag
end

Instance Method Details

#childrenObject



29
30
31
# File 'lib/cfml/tag.rb', line 29

def children
  false
end

#classesObject



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

#convertObject



38
39
40
# File 'lib/cfml/tag.rb', line 38

def convert
  Nokogiri::HTML.fragment(to_html).children.first
end

#gold?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/cfml/tag.rb', line 16

def gold?
  @attrs["gold"] ? @cfml : @cfml.at("./cf:gold")
end

#instructionsObject



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

#nameObject



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_htmlObject



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_liquidObject



61
62
63
# File 'lib/cfml/tag.rb', line 61

def to_liquid
  to_html
end

#to_sObject



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

def to_s
  @cfml.to_s
end

#validate?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/cfml/tag.rb', line 74

def validate?
  true
end

#validationsObject



65
66
67
# File 'lib/cfml/tag.rb', line 65

def validations
  @validations ||= @attrs["validations"].to_s.split(/ /).map {|v| "validates-#{v}"}
end

#valueObject



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")
  #Deal with a label only wrapper... kinda jank
  if tag == "label" && instructions
    tag = "div"
    parsed = "<label>#{parsed}</label>"
  end
  #Inserting instructions here makes it a non-variable input maybe...
  "<#{tag} class=\"#{wrapper_classes}\">#{parsed}#{instructions}</#{tag}>"
end

#wrapper_classesObject



24
25
26
27
# File 'lib/cfml/tag.rb', line 24

def wrapper_classes
  extras = @attrs["only-if"] ? "only-if:#{@attrs["only-if"]}" : ""
  "#{@tag} #{extras}".strip
end