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
19
20
21
# File 'lib/bisque/report.rb', line 7

def initialize(params={})
  @params = self.class.defaults.merge params
  @params.each do |k,v|
    @params[k] = v.call if v.is_a?(Proc)
  end
  @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

#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 = nil, &block) ⇒ Object



101
102
103
104
# File 'lib/bisque/report.rb', line 101

def default(key, val=nil, &block)
  @defaults ||= {}
  @defaults[key] = val || block
end

.defaults(hash = nil) ⇒ Object



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

def defaults(hash=nil)
  if hash
    @defaults ||= {}
    @defaults.merge! hash
  else
    @defaults || {}
  end
end

.optional(*keys) ⇒ Object



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

def optional(*keys)
  if keys
    @optionals ||= []
    @optionals = (@optionals + keys).uniq
  else
    @optionals || []
  end
end

.paramsObject



153
154
155
# File 'lib/bisque/report.rb', line 153

def params
  @params || []
end

.query(qstr = nil) ⇒ Object



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

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



145
146
147
148
149
150
151
# File 'lib/bisque/report.rb', line 145

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



134
135
136
137
138
139
140
141
142
143
# File 'lib/bisque/report.rb', line 134

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



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

def [](index)
  @converted[index]
end

#eachObject



23
24
25
26
27
28
29
30
31
# File 'lib/bisque/report.rb', line 23

def each
  if block_given?
    @converted.each do |r|
      yield r
    end
  else
    Enumerator.new self, :each
  end
end

#lastObject



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

def last
  @converted.last
end

#to_sObject



41
42
43
# File 'lib/bisque/report.rb', line 41

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