Sha256: 936dcc69e4d96fa0d69564943d8fbcb9b0def7fc3bc2c70d03e44bb3714b917d
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
require "json" require "jquery/version" module Kernel def jQuery(*args) JQuery::Object.new(:jQuery, *args) end end module JQuery class Object attr_accessor :label, :args, :prev, :next def initialize(label, *args) @label = label.to_s @args = args.map do |arg| case arg when String JSString.new(arg) when Numeric JSNumeric.new(arg) when Symbol JSVar.new(arg) when Array, Hash JSStruct.new(arg) when Proc JSFunction.new(arg) else raise ArgumentError.new("#{arg.class} is not supported") end end end def to_s current = self chain = [current] while current.prev current = current.prev chain.unshift(current) end result = [] chain.map do |obj| expr = '' expr << "#{obj.label}(" expr << obj.args.map { |arg| arg.to_s }.join(',') expr << ')' result << expr end result.join('.') end alias_method :to_str, :to_s def method_missing(method, *args) next_obj = self.class.new(method, *args) self.next = next_obj next_obj.prev = self next_obj end end class JSExpr attr_accessor :expr def initialize(expr) @expr = expr end end class JSString < JSExpr def to_s %Q|"#{expr}"| end end class JSNumeric < JSExpr def to_s expr end end class JSVar < JSExpr def to_s expr.to_s end end class JSStruct < JSExpr def to_s JSON.dump(expr) end end class JSFunction < JSExpr def to_s arg, expression = expr.call(self) "function (#{arg}) { #{expression} }" end def method_missing(method, *args) [method, *args] end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ruby-jquery-0.0.1 | lib/jquery.rb |