Class: Dart::Reflection::OrmModelResolver

Inherits:
AbstractResolver show all
Defined in:
lib/dart/reflection/orm_model_resolver.rb

Constant Summary collapse

QUERY_OPTIONS =
[:where, :order, :limit]
QUERY_OPTION_REGEXS =
[/WHERE\s*(?<where>.*)/, /ORDER\s+BY\s*(?<order>.*)/, /LIMIT\s*(?<limit>.*)/]

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ OrmModelResolver

Returns a new instance of OrmModelResolver.



10
11
12
# File 'lib/dart/reflection/orm_model_resolver.rb', line 10

def initialize(model_class)
  @this_model_class = model_class
end

Instance Method Details

#association_for(ass_name) ⇒ Association

Returns the association with the given ass_name or nil if one does not exist

Parameters:

  • ass_name (String)

Returns:



21
22
23
24
25
# File 'lib/dart/reflection/orm_model_resolver.rb', line 21

def association_for(ass_name)
  if ass_reflection = reflection_from(ass_name)
    build_association(ass_reflection)
  end
end

#build_from_association(association) ⇒ Object



14
15
16
# File 'lib/dart/reflection/orm_model_resolver.rb', line 14

def build_from_association(association)
  self.class.new(association.model_class)
end

#column_for(col_name) ⇒ String

Returns the column with the given col_name or nil if one does not exist

Parameters:

  • col_name (String)

Returns:

  • (String)


30
31
32
# File 'lib/dart/reflection/orm_model_resolver.rb', line 30

def column_for(col_name)
  col_name if has_column?(col_name)
end

#scope_hash_from(sql_string) ⇒ Object

Returns parts of the given sql_string in a hash of the form …, order: …, limit: …



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dart/reflection/orm_model_resolver.rb', line 45

def scope_hash_from(sql_string)
  # TODO: cleanup - this is some of the ugliest code I've ever written
  result = {}
  re_start_pairs = QUERY_OPTION_REGEXS.map {|re| [re, sql_string =~ re]}.reject {|_, i| i.nil?}.sort_by {|_, i| i}
  re_start_pairs.each_with_index do |re_start, i|
    re, start_index = re_start
    end_index = -1
    if next_match = re_start_pairs[i+1]
      end_index = next_match[1]-1
    end
    sql_string[start_index..end_index] =~ re
    QUERY_OPTIONS.each {|o| result[o] = $~[o].strip if ($~[o] rescue nil)}
  end

  result
end

#table_nameObject



34
35
36
# File 'lib/dart/reflection/orm_model_resolver.rb', line 34

def table_name
  this_model_class.table_name.to_s
end