Class: QueryDef

Inherits:
Object
  • Object
show all
Defined in:
lib/cuca/stdlib/listwidget/querydef.rb

Overview

QueryDef is a settings class that hold the settings for the query for listwidget

FIXME: Change name or put into namespace

Constant Summary collapse

ATTRIBS =
['order_by', 'order','range', 'filters']
ATTRIBS_ENCODE =
{ 
  'range' => 
  Proc.new do |e| 
    "#{e.first}-#{e.last}" 
  end,
 'filters' => 
  Proc.new do |e|  
    s = ''; 
    e.each_pair { |k,v| s << "#{k}:#{v};" }; 
    s 
  end,
  'order' => 
  Proc.new do |e|
    ['ASC', 'DESC'].include?(e) ? e : 'ASC'
  end
}
ATTRIBS_DECODE =
{
   'range' => 
   Proc.new do |e| 
      Range.new(e.split('-')[0].to_i,e.split('-')[1].to_i) 
   end,
   'filters' => 
   Proc.new do |e| 
      h = {}
      e.split(';').each do |p|
           vals = p.split(':')
           h[vals[0]] = vals[1] if (vals.size == 2)
      end
   h
   end,
   'order' => 
    Proc.new do |e|
      ['ASC', 'DESC'].include?(e) ? e : 'ASC'
    end
}
ATTRIBS_DEFAULTS =
{ 'range' => 0..9,
'order_by' => '',
'order' => 'ASC',
'filters' => {} }

Instance Method Summary collapse

Constructor Details

#initialize(list_name) ⇒ QueryDef

Returns a new instance of QueryDef.



165
166
167
168
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 165

def initialize(list_name)
 @list_name = list_name 
 @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *params) ⇒ Object

Raises:

  • (NoMethodError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 61

def method_missing(m, *params)
  met = m.id2name

  #getter 
  if (ATTRIBS.include?(met)) then
    return @data[met] || ATTRIBS_DEFAULTS[met].dup
  end

  #setter
  raise NoMethodError if met[met.size-1].chr != '='
  raise NoMethodError if params.size != 1
  met = met[0..met.size-2]		# cut '='
  if ATTRIBS.include?(met) then
    @data[met] = params[0]
  else
    raise NoMethodError
  end
end

Instance Method Details

#at_dec(name) ⇒ Object



109
110
111
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 109

def at_dec(name)
   name[@list_name.size..@name.size-1]
end

#at_enc(name) ⇒ Object

attribute-name enc/dec



105
106
107
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 105

def at_enc(name)
  "#{@list_name}_#{name}"
end

#escape(string) ⇒ Object

take from CGI class



50
51
52
53
54
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 50

def escape(string)
    string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
     '%' + $1.unpack('H2' * $1.size).join('%').upcase
   end.tr(' ', '+')
end

#ev_dec(name, value) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 90

def ev_dec(name, value)
  if ATTRIBS_DECODE.has_key?(name) then
#     $stderr.puts "ev_dec #{name} #{value}"
    begin
       return ATTRIBS_DECODE[name].call(value)
    rescue
#        $stderr.puts "Decoding failed: #{name} - #{value}: #{$!}"
       return ATTRIBS_DEFAULTS[name].dup
    end
  else
    return value
  end
end

#ev_enc(name) ⇒ Object

value enc/dec



81
82
83
84
85
86
87
88
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 81

def ev_enc(name)
#   $stderr.puts "env_enc(#{name}) #{@data.inspect}"
  if ATTRIBS_ENCODE.has_key?(name) then
    ATTRIBS_ENCODE[name].call(@data[name] || '')
  else
    @data[name]
  end
end

#from_params(params) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 133

def from_params(params)
#   $stderr.puts "CGI PARAMS: #{$cgi.params.inspect}"
#   $stderr.puts "*** From Params\n #{params.inspect}\n"
#   $stderr.puts "*** DEFAULT ATTR: #{ATTRIBS_DEFAULTS.inspect}\n"
  ATTRIBS.each do |a|
#     $stderr.puts "Checking: #{at_enc(a)} = #{params[at_enc(a)][0]}"
    v = params[at_enc(a)]
    if v then 
      @data[a] = ev_dec(a,v)
    else
      @data[a] = ATTRIBS_DEFAULTS[a].dup
    end
#     $stderr.puts "\n*** ENDFROM_PARAMS: #{@data.inspect}"
  end

  # checking on filters that come by post
#   $stderr.puts "PARAMS: #{params.inspect}"
  params.each_pair do |p,v|
     if p.include?("#{@list_name}_filter_") then
        @data['filters'] ||= {}
        fname = p.scan(/\_filter\_(.*)/)[0][0]
        fdata = v
        @data['filters'][fname] = fdata
#         $stderr.puts "Filter update: #{@data['filters'].inspect}"
     end
  end

 
#   $stderr.puts @data.to_yaml
  return self
end

#to_h(attr = nil, newval = nil, attr2 = nil, newval2 = nil) ⇒ Object

returns an hash with the values and escaped names and attributes attr and newval with swap a value for this return without changing the actual data



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 115

def to_h(attr=nil, newval=nil, attr2=nil, newval2=nil)
  if attr then
     @backup = @data.dup
     @data[attr] = newval
     @data[attr2] = newval2 unless attr2.nil?
  end

  u = {}
  ATTRIBS.each do |a|
#      $stderr.puts "Encoding: #{a} - #{@data[a]}"
     u[at_enc(a)] = escape(ev_enc(a) || '')
  end
  
  if attr then @data = @backup end

  return u
end

#unescape(string) ⇒ Object



55
56
57
58
59
# File 'lib/cuca/stdlib/listwidget/querydef.rb', line 55

def unescape(string)
   string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
     [$1.delete('%')].pack('H*')
   end
end