Class: Ronin::Code::SQL::Field

Inherits:
Object
  • Object
show all
Includes:
Emittable, Operators
Defined in:
lib/ronin/code/sql/field.rb

Overview

Represents a SQL column, table or database name.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Emittable

#emitter, #inspect, #to_s, #to_sql

Methods included from Operators

#!, #!=, #%, #&, #*, #+, #+@, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #and, #as, #glob, #in, #is, #is_not, #like, #match, #not, #or, #regexp, #|, #~

Constructor Details

#initialize(name, parent = nil) ⇒ Field

Initializes the new field.

Parameters:

  • name (String)

    The name of the field.

  • parent (Field, nil) (defaults to: nil)

    The parent of the field.



56
57
58
59
# File 'lib/ronin/code/sql/field.rb', line 56

def initialize(name,parent=nil)
  @name   = name.to_s
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Field (protected)

Allows accessing columns from tables or tables from databases.

Examples:

db.users
users.id

Parameters:

  • name (Symbol)

    The sub-field name.

  • arguments (Array)

    Additional method arguments.

Returns:

  • (Field)

    The sub-field for the given name.

Raises:

  • (ArgumentError)

    The method missing call was given additional arguments.

  • (NoMethodError)

    Cannot access a column from another column.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ronin/code/sql/field.rb', line 120

def method_missing(name,*arguments)
  unless arguments.empty?
    raise(ArgumentError,"canot access columns or tables with arguments")
  end

  if (self.parent.nil? || self.parent.parent.nil?)
    Field.new(name,self)
  else
    raise(NoMethodError,"cannot access columns from other columns")
  end
end

Instance Attribute Details

#nameString (readonly)

The name of the field.

Returns:

  • (String)


40
41
42
# File 'lib/ronin/code/sql/field.rb', line 40

def name
  @name
end

#parentField? (readonly)

The parent of the field name.

Returns:



45
46
47
# File 'lib/ronin/code/sql/field.rb', line 45

def parent
  @parent
end

Class Method Details

.parse(name) ⇒ Field

Parses a field.

Parameters:

  • name (String)

Returns:

  • (Field)

    The parsed field.



69
70
71
72
73
74
75
76
# File 'lib/ronin/code/sql/field.rb', line 69

def self.parse(name)
  names = name.to_s.split('.',3)
  field = nil

  names.each { |keyword| field = new(keyword,field) }

  return field
end

Instance Method Details

#respond_to_missing?(name) ⇒ Boolean

Determines if the field responds to the given method.

Parameters:

  • name (Symbol)

    The method name.

Returns:

  • (Boolean)

    Will return false if the field already has two parents, otherwise will return true.



90
91
92
# File 'lib/ronin/code/sql/field.rb', line 90

def respond_to_missing?(name)
  self.parent.nil? || self.parent.parent.nil?
end