Class: ArelExtensions::Nodes::Case

Inherits:
Arel::Nodes::Case
  • Object
show all
Includes:
Arel::Expressions, Arel::Math, Arel::OrderPredications, Arel::Predications, Comparators, Math, MathFunctions, ArelExtensions::NullFunctions, Predications, StringFunctions
Defined in:
lib/arel_extensions/nodes/case.rb,
lib/arel_extensions/nodes/case.rb,
lib/arel_extensions/nodes/case.rb

Defined Under Namespace

Classes: Else, When

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArelExtensions::NullFunctions

#coalesce, #is_not_null, #is_null

Methods included from StringFunctions

#&, #[], #ai_collate, #ai_imatches, #ai_matches, #blank, #ci_collate, #collate, #concat, #downcase, #edit_distance, #group_concat, #idoes_not_match, #idoes_not_match_all, #idoes_not_match_any, #imatches, #imatches_all, #imatches_any, #length, #levenshtein_distance, #locate, #ltrim, #md5, #not_blank, #regexp_replace, #repeat, #replace, #rtrim, #smatches, #soundex, #substring, #trim, #upcase

Methods included from MathFunctions

#*, #/, #abs, #ceil, #floor, #format_number, #log10, #pow, #power, #round, #std, #sum, #variance

Methods included from Predications

#cast, #convert_to_node, #imatches, #in, #matches, #not_in

Methods included from Comparators

#!~, #<, #<=, #=~, #>, #>=

Methods included from Math

#+, #-

Constructor Details

#initialize(expression = nil, default = nil) ⇒ Case

Returns a new instance of Case.



11
12
13
14
15
# File 'lib/arel_extensions/nodes/case.rb', line 11

def initialize expression = nil, default = nil
  @case = expression
  @conditions = []
  @default = default
end

Instance Attribute Details

#caseObject

Returns the value of attribute case.



9
10
11
# File 'lib/arel_extensions/nodes/case.rb', line 9

def case
  @case
end

#conditionsObject

Returns the value of attribute conditions.



9
10
11
# File 'lib/arel_extensions/nodes/case.rb', line 9

def conditions
  @conditions
end

#defaultObject

Returns the value of attribute default.



9
10
11
# File 'lib/arel_extensions/nodes/case.rb', line 9

def default
  @default
end

Instance Method Details

#as(other) ⇒ Object



104
105
106
# File 'lib/arel_extensions/nodes/case.rb', line 104

def as other
  Arel::Nodes::As.new self, Arel::Nodes::SqlLiteral.new(other)
end

#else(expression) ⇒ Object



80
81
82
83
# File 'lib/arel_extensions/nodes/case.rb', line 80

def else expression
  @default = Case::Else.new expression
  self
end

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

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/arel_extensions/nodes/case.rb', line 96

def eql? other
  self.class == other.class &&
    self.case == other.case &&
    self.conditions == other.conditions &&
    self.default == other.default
end

#hashObject



92
93
94
# File 'lib/arel_extensions/nodes/case.rb', line 92

def hash
  [@case, @conditions, @default].hash
end

#initialize_copy(other) ⇒ Object



85
86
87
88
89
90
# File 'lib/arel_extensions/nodes/case.rb', line 85

def initialize_copy other
  super
  @case = @case.clone if @case
  @conditions = @conditions.map { |x| x.clone }
  @default = @default.clone if @default
end

#return_typeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/arel_extensions/nodes/case.rb', line 44

def return_type
  obj = if @conditions.length > 0
      @conditions.last.right
    elsif @default
      @default.expr
    end
  if obj.respond_to?(:return_type)
    obj.return_type
  else
    case obj
    when Integer, Float
      :number
    when Date, DateTime,Time
      :datetime
    when Arel::Attributes::Attribute
      begin
        Arel::Table.engine.connection.schema_cache.columns_hash(obj.relation.table_name)[obj.name.to_s].type
      rescue Exception
        :string
      end
    else
      :string
    end
  end
end

#then(expression) ⇒ Object



75
76
77
78
# File 'lib/arel_extensions/nodes/case.rb', line 75

def then expression
  @conditions.last.right = expression
  self
end

#when(condition, expression = nil) ⇒ Object



70
71
72
73
# File 'lib/arel_extensions/nodes/case.rb', line 70

def when condition, expression = nil
  @conditions << Case::When.new(condition, expression)
  self
end