Class: ArelExtensions::Nodes::Case

Inherits:
Arel::Nodes::Case show all
Includes:
Arel::Expressions, Arel::Math, Arel::OrderPredications, Arel::Predications, Aliases, Comparators, DateDuration, 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 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 Predications

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

Methods included from ArelExtensions::NullFunctions

#coalesce, #coalesce_blank, #if_present, #is_not_null, #is_null

Methods included from MathFunctions

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

Methods included from Math

#+, #-

Methods included from DateDuration

#day, #format, #format_date, #hour, #minute, #month, #second, #wday, #week, #year

Methods included from Comparators

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

Methods included from Aliases

#xas

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



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

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

#else(expression) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#initialize_copy(other) ⇒ Object



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

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

#return_typeObject



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

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

#then(expression) ⇒ Object



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

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

#when(condition, expression = nil) ⇒ Object



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

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