Module: Mochigome

Defined in:
lib/query.rb,
lib/relation.rb,
lib/data_node.rb,
lib/exceptions.rb,
lib/formatting.rb,
lib/model_graph.rb,
lib/ordered_set.rb,
lib/mochigome_ver.rb,
lib/subgroup_model.rb,
lib/model_extensions.rb,
lib/arel_rails2_hacks.rb

Defined Under Namespace

Modules: Formatting, ModelExtensions Classes: AggregationSettings, AssociationError, ColumnsHashProxy, DataNode, DataNodeError, InvalidLayerError, ModelGraph, ModelSetupError, OrderedSet, Query, QueryError, QueryLine, Relation, ReportFocus, ReportFocusSettings, SubgroupModel, SubgroupPseudoRecord, SubgroupPseudoRecordReportFocus

Constant Summary collapse

VERSION =
"0.2.5"

Class Method Summary collapse

Class Method Details

.case_expr(table_pred, then_val, else_val) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/model_extensions.rb', line 115

def self.case_expr(table_pred, then_val, else_val)
  lambda {|t|
    Arel::Nodes::SqlLiteral.new(
      "(CASE WHEN #{arel_exprify(table_pred, t)} " +
      "THEN #{arel_exprify(then_val, t)} " +
      "ELSE #{arel_exprify(else_val, t)} END)"
    )
  }
end

.multi_case_expr(cond_results, else_val = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/model_extensions.rb', line 125

def self.multi_case_expr(cond_results, else_val = nil)
  lambda {|t|
    Arel::Nodes::SqlLiteral.new(
      "(CASE " +
      cond_results.map{|cond, result|
        "WHEN #{arel_exprify(cond, t)} THEN #{arel_exprify(result, t)}"
      }.join(" ") +
      (else_val ? "ELSE #{arel_exprify(else_val, t)}" : "") +
      "END)"
    )
  }
end

.null_unless(pred, value_func) ⇒ Object

FIXME All this lambda{|t| stuff is just needlessly confusing for the little good that it accomplishes.



107
108
109
110
111
112
113
# File 'lib/model_extensions.rb', line 107

def self.null_unless(pred, value_func)
  case_expr(
    lambda {|t| pred.call(value_func.call(t))},
    value_func,
    Arel::Nodes::SqlLiteral.new("NULL")
  )
end

.sql_bool_to_string(attr, prefix = "") ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/model_extensions.rb', line 138

def self.sql_bool_to_string(attr, prefix = "")
  case_expr(
    Arel::Nodes::NamedFunction.new('lower', [attr]).
      in(["true", "t", 1, "1", "y", "yes"]),
    "#{prefix}Yes",
    "#{prefix}No"
  )
end