Class: Piglet::Field::Reference

Inherits:
Object
  • Object
show all
Includes:
Field
Defined in:
lib/piglet/field/reference.rb

Overview

:nodoc:

Constant Summary

Constants included from Field

Field::FUNCTIONS, Field::SYMBOLIC_OPERATORS

Instance Attribute Summary

Attributes included from Field

#name, #predecessors, #type

Instance Method Summary collapse

Methods included from Field

#and, #as, #cast, #diff, #distinct, #empty?, #field, #field_alias, #filter, #flatten, #generate_field_alias, #get, #limit, #matches, #ne, #neg, #not, #not_null?, #null?, #or, #order, #sample

Constructor Details

#initialize(name, relation = nil, options = nil) ⇒ Reference

Returns a new instance of Reference.



8
9
10
11
12
13
14
# File 'lib/piglet/field/reference.rb', line 8

def initialize(name, relation=nil, options=nil)
  options ||= {}
  @name, @parent = name, relation
  @explicit_ancestry = options[:explicit_ancestry] || false
  @type = options[:type]
  @predecessors = [relation] unless relation.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/piglet/field/reference.rb', line 20

def method_missing(name, *args)
  if name.to_s =~ /^\w+$/ && args.empty?
    field(name)
  else
    super
  end
end

Instance Method Details

#[](n) ⇒ Object



28
29
30
# File 'lib/piglet/field/reference.rb', line 28

def [](n)
  field("\$#{n}")
end

#simple?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/piglet/field/reference.rb', line 16

def simple?
  true
end

#to_s(inner = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/piglet/field/reference.rb', line 32

def to_s(inner=false)
  if @explicit_ancestry
    if @parent.respond_to?(:alias)
      "#{@parent.alias}.#{@name.to_s}"
    else
      expr = if inner then @parent.field_alias else @parent end
      "#{expr}.#{@name.to_s}"
    end
  else
    @name.to_s
  end
end