class Conjoin::JQuery

Constants

JS_ESCAPE

Attributes

html[RW]
options[RW]
selector[RW]

Public Class Methods

escape(js) click to toggle source
# File lib/conjoin/jquery.rb, line 46
def self.escape js
  js.to_s.gsub(/(\\|<\/|\r\n|\\3342\\2200\\2250|[\n\r"'])/) {|match| JS_ESCAPE[match] }
end
new(selector, options = {}) click to toggle source
# File lib/conjoin/jquery.rb, line 7
def initialize selector, options = {}
  @html     = ''
  @selector = selector
  @options  = options

  @options.empty? \
    ? @html += "$('#{selector}')" \
    : @html += "$('#{selector}', #{options.to_json})"
end

Public Instance Methods

escape(js) click to toggle source
# File lib/conjoin/jquery.rb, line 42
def escape js
  Conjoin::JQuery.escape js
end
method_missing(m, *args, &block) click to toggle source
# File lib/conjoin/jquery.rb, line 17
def method_missing(m, *args, &block)
  elem = args.first

  case elem
  when JQuery
    content = elem.to_s.chomp(';')
  when String
    content = "'#{escape elem}'"
  when Hash, OpenStruct
    content = elem.to_json
  else
    content = elem
  end

  @html += ".#{m.to_s.camelize(:lower)}(#{content})"

  self
end
to_s() click to toggle source
# File lib/conjoin/jquery.rb, line 36
def to_s
  return_html = html.dup.to_s
  @html       = "$('#{selector}')"
  "#{return_html};"
end