Class: CML::Converters::JSAwesome

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

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ JSAwesome

Returns a new instance of JSAwesome.



4
5
6
7
8
9
# File 'lib/cml/converters/jsawesome.rb', line 4

def initialize(json)
  parsed = JSON.parse(json)
  @json = parsed[0] || []
  @labels = parsed[1] || {}
  @tags = []
end

Instance Method Details

#convertObject



11
12
13
14
15
16
17
18
19
# File 'lib/cml/converters/jsawesome.rb', line 11

def convert
  @json.map do |k,v|
    if k.is_a?(Array)
      (k << v).map {|k,v| process(k,v)}
    else
      process(k,v)
    end
  end.join("\n") + meta
end

#label(k) ⇒ Object



30
31
32
33
34
35
# File 'lib/cml/converters/jsawesome.rb', line 30

def label(k)
  k = k.to_s.sub(/^[_#*^~]?\+?/,'')
  l = @labels[k]
  label = l.is_a?(Hash) ? l["label"] : l if l
  label ||= k.capitalize.gsub(/_/,' ')
end

#metaObject



21
22
23
24
25
26
27
28
# File 'lib/cml/converters/jsawesome.rb', line 21

def meta
  (@labels["gold"] || {}).select do |k,v|
    next if k =~ /^_/
    !@tags.map {|t| t.name }.include?(k)
  end.map do |k,v|
    tag("meta", k, {:meta => true})
  end.compact.join("\n")
end

#process(k, v) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cml/converters/jsawesome.rb', line 68

def process(k,v)
  select_default = @labels['{}'] || "Choose Category"
  cml = case v
  #a select tag or group of radios/checkboxes
  when Array
    if(k =~ /^[^_]/)
      if $~[0] == "*"
        parent = "radios"
        child = "radio"
      elsif $~[0] == "^"
        parent = "checkboxes"
        child = "checkbox"
      else
        parent = "select"
        child = "option"
      end
      tag(parent, k, {:children => v.map { |c| [child, c, {:nested => child}]}, :parent => parent })
    #Multiple hidden fields...
    elsif(k =~ /^_/)
      v.map { |c| tag("hidden", c, :parent => true)}.join("\n")
    end
  else
    if(k =~ /^#/)
      tag("textarea", k, {"default" => v, :parent => true})
    elsif(k =~ /^_/)
      tag("hidden", k, {"value" => v, :parent => true})
    elsif(k =~ /^\^/)
      tag("checkbox", k, {"default" => v, :parent => true})
    else
      tag("text", k, {"default" => v, :parent => true})
    end
  end
end

#tag(tag, val, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cml/converters/jsawesome.rb', line 37

def tag(tag, val, opts = {})
  val = Array(val)
  return nil unless val[0]
  parts = val[0].split("|")
  key = parts[0].to_s.sub(/^[_#*^~]?\+?/,'')
  label = label(parts[0])
  parts << key if label != key.capitalize.gsub(/_/,' ') || opts[:meta]
  attrs = opts[:meta] ? [] : ["label=\"#{label}\""]
  attrs << "#{opts[:nested] ? "value" : "name"}=\"#{parts[1]}\"" if parts[1]
  attrs << "checked=\"true\"" if val[1]
  attrs << "validates=\"required\"" if @labels[key] && @labels[key]["required"]
  opts.reject {|k,v| v.to_s.strip.length == 0 || k.is_a?(Symbol) }.each do |k,v|
    attrs << "#{k}=\"#{v}\""
  end
  if @labels["gold"] && @labels["gold"][key]
    opts["gold"] = {}
    src = @labels["gold"][key]
    opts["gold"]["src"] = src
    opts["gold"]["regex"], opts["gold"]["flags"] = Array(@labels["gold"]["_"+src+"_regex"])
    opts["gold"]["strict"] = @labels["gold"]["_"+src+"_strict"]
  else
    opts["gold"] = false
  end
  
  tag = Parser.tag_class(tag).new("<cml:#{tag} #{attrs.join(" ")} />")
  tag.gold = opts["gold"]
  opts[:children].each { |c| tag.cml.add_child(tag(*c).cml) } if opts[:children]
  @tags << tag if opts[:parent]
  tag
end