Class: Bisque::Report

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bisque/report.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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?
    @sql.gsub!(/(?<!:):#{param}/, sanitize_and_sqlize(value))
  end
  @results = ActiveRecord::Base.connection.execute @sql
  extract_datatypes
  construct_converted
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/bisque/report.rb', line 5

def params
  @params
end

#sqlObject

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

.paramsObject



141
142
143
# File 'lib/bisque/report.rb', line 141

def params
  @params || []
end

.query(qstr = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/bisque/report.rb', line 112

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_classObject



133
134
135
136
137
138
139
# File 'lib/bisque/report.rb', line 133

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



122
123
124
125
126
127
128
129
130
131
# File 'lib/bisque/report.rb', line 122

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

#eachObject



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

#lastObject



34
35
36
# File 'lib/bisque/report.rb', line 34

def last
  @converted.last
end

#to_sObject



38
39
40
# File 'lib/bisque/report.rb', line 38

def to_s
  "#{self.class}: #{count} result#{'s' if count != 1}"
end