Class: CML::Tag

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

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

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#cmlObject (readonly)

Returns the value of attribute cml.



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

def cml
  @cml
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

Instance Method Details

#childrenObject



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

def children
  false
end

#classesObject



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

Returns:

  • (Boolean)


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

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

#instructionsObject



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

#legendObject



93
94
95
# File 'lib/cml/tag.rb', line 93

def legend
  label("h2")
end

#nameObject



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_labelObject



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_htmlObject



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_liquidObject



97
98
99
# File 'lib/cml/tag.rb', line 97

def to_liquid
  to_html
end

#to_sObject



79
80
81
# File 'lib/cml/tag.rb', line 79

def to_s
  @cml.to_s
end

#validate?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/cml/tag.rb', line 110

def validate?
  true
end

#validationsObject



101
102
103
# File 'lib/cml/tag.rb', line 101

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

#valueObject



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")
  #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



55
56
57
58
# File 'lib/cml/tag.rb', line 55

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