Class: JQuery::Object

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, *args) ⇒ Object

Returns a new instance of Object.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jquery.rb', line 14

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



57
58
59
60
61
62
# File 'lib/jquery.rb', line 57

def method_missing(method, *args)
  next_obj    = self.class.new(method, *args)
  self.next   = next_obj
  next_obj.prev = self
  next_obj
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



12
13
14
# File 'lib/jquery.rb', line 12

def args
  @args
end

#labelObject

Returns the value of attribute label.



12
13
14
# File 'lib/jquery.rb', line 12

def label
  @label
end

#nextObject

Returns the value of attribute next.



12
13
14
# File 'lib/jquery.rb', line 12

def next
  @next
end

#prevObject

Returns the value of attribute prev.



12
13
14
# File 'lib/jquery.rb', line 12

def prev
  @prev
end

Instance Method Details

#to_sObject Also known as: to_str



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jquery.rb', line 34

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