slim-attributes 0.3 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/slim_attributes.rb +56 -0
  2. metadata +2 -2
@@ -28,6 +28,14 @@ class Mysql::Result
28
28
  to_hash.to_a
29
29
  end
30
30
 
31
+ # Load up all the attributes before a freeze
32
+ alias_method :regular_freeze, :freeze
33
+
34
+ def freeze
35
+ to_hash.freeze
36
+ regular_freeze
37
+ end
38
+
31
39
  def method_missing(name, *args, &block)
32
40
  to_hash.send(name, *args, &block)
33
41
  end
@@ -35,3 +43,51 @@ class Mysql::Result
35
43
  end
36
44
 
37
45
  require 'slim_attrib_ext'
46
+
47
+ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
48
+ def select(sql, name = nil)
49
+ fields, rows = select_raw(sql, name)
50
+ result = []
51
+ for row in rows
52
+ row_hash = {}
53
+ fields.each_with_index do |f, i|
54
+ row_hash[f] = row[i]
55
+ end
56
+ result << row_hash
57
+ end
58
+ result
59
+ end
60
+
61
+ def select_raw(sql, name = nil)
62
+ res = execute(sql, name)
63
+ results = result_as_array(res)
64
+ fields = []
65
+ rows = []
66
+ if res.ntuples > 0
67
+ fields = res.fields
68
+ results.each do |row|
69
+ row.each_index do |cell_index|
70
+ # If this is a money type column and there are any currency symbols,
71
+ # then strip them off. Indeed it would be prettier to do this in
72
+ # PostgreSQLColumn.string_to_decimal but would break form input
73
+ # fields that call value_before_type_cast.
74
+ if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID
75
+ # Because money output is formatted according to the locale, there are two
76
+ # cases to consider (note the decimal separators):
77
+ # (1) $12,345,678.12
78
+ # (2) $12.345.678,12
79
+ case column = row[cell_index]
80
+ when /^-?\D+[\d,]+\.\d{2}$/ # (1)
81
+ row[cell_index] = column.gsub(/[^-\d\.]/, '')
82
+ when /^-?\D+[\d\.]+,\d{2}$/ # (2)
83
+ row[cell_index] = column.gsub(/[^-\d,]/, '').sub(/,/, '.')
84
+ end
85
+ end
86
+ end
87
+ rows << row
88
+ end
89
+ end
90
+ res.clear
91
+ return fields, rows
92
+ end
93
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: slim-attributes
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2008-04-21 00:00:00 +03:00
6
+ version: "0.4"
7
+ date: 2008-05-18 00:00:00 +03:00
8
8
  summary: Slim attributes boosts speed in Rails/Mysql ActiveRecord Models by avoiding instantiating Hashes for each result row, and lazily instantiating attributes as needed
9
9
  require_paths:
10
10
  - lib