Class: Dbee::Query::KeyPath

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dbee/query/key_path.rb

Overview

This class represents a relative path from a model to a column. For example: Say we have a model called “users” which is represented by a “users” table. The “users” table also has a one-to-many relationship with a “phone_numbers” table, which is modeled as a nested model under “users” as “phone_numbers”. Then, to get the column: “area_code”, you would use: “phone_numbers.area_code”. Say the column “name” is located on “users”, you could use the key path: “name”. This also works for deeper nested columns in the same fashion.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ KeyPath

Returns a new instance of KeyPath.



36
37
38
39
40
41
42
43
44
# File 'lib/dbee/query/key_path.rb', line 36

def initialize(value)
  raise 'Value is required' if value.to_s.empty?

  @value          = value.to_s
  @ancestor_names = value.to_s.split(SPLIT_CHAR)
  @column_name    = @ancestor_names.pop

  freeze
end

Instance Attribute Details

#ancestor_namesObject (readonly)

Returns the value of attribute ancestor_names.



32
33
34
# File 'lib/dbee/query/key_path.rb', line 32

def ancestor_names
  @ancestor_names
end

#column_nameObject (readonly)

Returns the value of attribute column_name.



32
33
34
# File 'lib/dbee/query/key_path.rb', line 32

def column_name
  @column_name
end

#valueObject (readonly)

Returns the value of attribute value.



32
33
34
# File 'lib/dbee/query/key_path.rb', line 32

def value
  @value
end

Class Method Details

.get(obj) ⇒ Object



23
24
25
# File 'lib/dbee/query/key_path.rb', line 23

def get(obj)
  obj.is_a?(self.class) ? obj : new(obj)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



50
51
52
# File 'lib/dbee/query/key_path.rb', line 50

def ==(other)
  other.to_s == to_s
end

#hashObject



46
47
48
# File 'lib/dbee/query/key_path.rb', line 46

def hash
  value.hash
end