Class: Squeel::DSL

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

Overview

Interprets DSL blocks, generating various Squeel nodes as appropriate.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller_binding) ⇒ DSL (private)

This isn’t normally called directly, but via DSL.eval, which will pass the block’s binding to the new instance, for use with #my.

Parameters:

  • The (Binding)

    block’s binding.



41
42
43
# File 'lib/squeel/dsl.rb', line 41

def initialize(caller_binding)
  @caller = caller_binding.eval 'self'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#node_nameNodes::Stub (private) #node_name(klass) ⇒ Nodes::Join (private) #node_name(first_arg, *other_args) ⇒ Nodes::Function (private)

Node generation inside DSL blocks.

Overloads:

  • #node_nameNodes::Stub

    Creates a Stub. Method calls chained from this Stub will determine what type of node we eventually end up with.

    Returns:

  • #node_name(klass) ⇒ Nodes::Join

    Creates a Join with a polymorphic class matching the given parameter

    Parameters:

    • klass (Class)

      The polymorphic class of the join node

    Returns:

    • (Nodes::Join)

      A join node with the name of the method and the given class

  • #node_name(first_arg, *other_args) ⇒ Nodes::Function

    Creates a Function with the given arguments becoming the function’s arguments

    Parameters:

    • first_arg

      The first argument

    • *other_args

      Optional additional arguments

    Returns:

    • (Nodes::Function)

      A function node for the given method name with the given arguments



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/squeel/dsl.rb', line 73

def method_missing(method_id, *args)
  super if method_id == :to_ary

  if args.empty?
    Nodes::Stub.new method_id
  elsif (args.size == 1) && (Class === args[0])
    Nodes::Join.new(method_id, Arel::InnerJoin, args[0])
  else
    Nodes::Function.new method_id, args
  end
end

Class Method Details

.eval {|dsl| ... } ⇒ Object

Called from an adapter, not directly. Evaluates a block of Squeel DSL code.

Examples:

A DSL block that uses instance_eval

Post.where{title == 'Hello world!'}

A DSL block with access to methods from the closure

Post.where{|dsl| dsl.title == local_method(local_var)}

Yields:

  • (dsl)

    A block of Squeel DSL code, with an optional argument if access to closure methods is desired.

Returns:

  • The results of the interpreted DSL code.



27
28
29
30
31
32
33
# File 'lib/squeel/dsl.rb', line 27

def self.eval(&block)
  if block.arity > 0
    yield self.new(block.binding)
  else
    self.new(block.binding).instance_eval(&block)
  end
end

Instance Method Details

#my(&block) ⇒ Object (private)

If you really need to get at an instance variable or method inside a DSL block, this method will let you do it. It passes a block back to the DSL’s caller for instance_eval.

It’s also pretty evil, so I hope you enjoy using it while I’m burning in programmer hell.

Parameters:

  • &block

    A block to instance_eval against the DSL’s caller.

Returns:



54
55
56
# File 'lib/squeel/dsl.rb', line 54

def my(&block)
  @caller.instance_eval &block
end