Class: ArelExtensions::Nodes::Case

Inherits:
Arel::Nodes::Case
  • Object
show all
Includes:
Arel::Expressions, Arel::Math, Arel::OrderPredications, Arel::Predications, Aliases, 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, #byte_length, #char_length, #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

#+, #-

Methods included from Aliases

#xas

Constructor Details

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

Returns a new instance of Case.



13
14
15
16
17
# File 'lib/arel_extensions/nodes/case.rb', line 13

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

Instance Attribute Details

#caseObject

Returns the value of attribute case.



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

def case
  @case
end

#conditionsObject

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#defaultObject

Returns the value of attribute default.



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

def default
  @default
end

Instance Method Details

#as(other) ⇒ Object



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

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

#else(expression) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#initialize_copy(other) ⇒ Object



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

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

#return_typeObject



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

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
      ArelExtensions::column_of(obj.relation.table_name, obj.name.to_s)&.type || :string
    else
      :string
    end
  end
end

#then(expression) ⇒ Object



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

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

#when(condition, expression = nil) ⇒ Object



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

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