Class: Bisque::Report
- Inherits:
-
Object
- Object
- Bisque::Report
- Includes:
- Enumerable
- Defined in:
- lib/bisque/report.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#sql ⇒ Object
Returns the value of attribute sql.
Class Method Summary collapse
- .default(key, val) ⇒ Object
- .defaults(hash = nil) ⇒ Object
- .optional(*keys) ⇒ Object
- .params ⇒ Object
- .query(qstr = nil) ⇒ Object
- .row_class ⇒ Object
- .rows(&block) ⇒ Object
Instance Method Summary collapse
- #[](index) ⇒ Object
- #each ⇒ Object
-
#initialize(params = {}) ⇒ Report
constructor
A new instance of Report.
- #last ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Report
Returns a new instance of Report.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/bisque/report.rb', line 7 def initialize(params={}) @params = self.class.defaults.merge params @sql = self.class.query.dup self.class.params.each do |param| value = @params[param] raise Bisque::MissingParameterException, "Missing parameter :#{param} for construction of #{self.class} - please provide a value for this parameter to the constructor or define a default." if value.nil? && !self.class.optional.include?(param.intern) @sql.gsub!(/(?<!:):#{param}/, sanitize_and_sqlize(value)) end @results = ActiveRecord::Base.connection.execute @sql extract_datatypes construct_converted end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/bisque/report.rb', line 5 def params @params end |
#sql ⇒ Object
Returns the value of attribute sql.
5 6 7 |
# File 'lib/bisque/report.rb', line 5 def sql @sql end |
Class Method Details
.default(key, val) ⇒ Object
98 99 100 101 |
# File 'lib/bisque/report.rb', line 98 def default(key, val) @defaults ||= {} @defaults[key] = val end |
.defaults(hash = nil) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/bisque/report.rb', line 103 def defaults(hash=nil) if hash @defaults ||= {} @defaults.merge! hash else @defaults || {} end end |
.optional(*keys) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/bisque/report.rb', line 112 def optional(*keys) if keys @optionals ||= [] @optionals = (@optionals + keys).uniq else @optionals || [] end end |
.params ⇒ Object
150 151 152 |
# File 'lib/bisque/report.rb', line 150 def params @params || [] end |
.query(qstr = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/bisque/report.rb', line 121 def query(qstr=nil) if qstr @qstr = qstr.strip @params = qstr.scan(/(?<!:):\w+/).map { |p| p.gsub(/:/,'').intern } else raise Bisque::MissingQueryException, "Bisque Report #{self} missing query definition." if @qstr.nil? @qstr end end |
.row_class ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/bisque/report.rb', line 142 def row_class return Bisque::ReportRow if @row_class.nil? names = @row_class.split('::') names.reduce(Object) do |mod, name| mod.const_defined?(name) ? mod.const_get(name) : mod.const_missing(name) end end |
.rows(&block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bisque/report.rb', line 131 def rows(&block) if block_given? carray = self.to_s.split(/::/) carray.last << 'Row' @row_class = carray.join('::') c = Class.new(Bisque::ReportRow) c.class_eval(&block) Object.const_set @row_class, c end end |
Instance Method Details
#[](index) ⇒ Object
30 31 32 |
# File 'lib/bisque/report.rb', line 30 def [](index) @converted[index] end |
#each ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/bisque/report.rb', line 20 def each if block_given? @converted.each do |r| yield r end else Enumerator.new self, :each end end |
#last ⇒ Object
34 35 36 |
# File 'lib/bisque/report.rb', line 34 def last @converted.last end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/bisque/report.rb', line 38 def to_s "#{self.class}: #{count} result#{'s' if count != 1}" end |