Module: Valium
- Defined in:
- lib/valium.rb,
lib/valium/version.rb
Defined Under Namespace
Modules: ValueOf
Constant Summary collapse
- CollectionProxy =
we’re on 3.1+, yay for coder.load!
ActiveRecord::Associations::CollectionProxy
- VERSION =
"0.5.0"
Instance Method Summary collapse
- #valium_cast(value, column, coder_or_klass) ⇒ Object
- #valium_deserialize(value, coder) ⇒ Object
- #valium_select_multiple(attr_names) ⇒ Object
- #valium_select_one(attr_name) ⇒ Object
-
#value_of(*attr_names) ⇒ Object
(also: #values_of)
Minor version check.
Instance Method Details
#valium_cast(value, column, coder_or_klass) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/valium.rb', line 78 def valium_cast(value, column, coder_or_klass) if value.nil? || !column value elsif coder_or_klass valium_deserialize(value, coder_or_klass) else column.type_cast(value) end end |
#valium_deserialize(value, coder) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/valium.rb', line 15 def valium_deserialize(value, klass) if value.is_a?(String) && value =~ /^---/ result = YAML::load(value) rescue value if result.nil? || result.is_a?(klass) result else raise SerializationTypeMismatch, "Expected a #{klass}, but was a #{result.class}" end else value end end |
#valium_select_multiple(attr_names) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/valium.rb', line 54 def valium_select_multiple(attr_names) columns = attr_names.map {|n| columns_hash[n]} coders = attr_names.map {|n| serialized_attributes[n]} connection.select_rows( except(:select).select(attr_names.map {|n| arel_table[n]}).to_sql ).map! do |values| values.each_with_index do |value, index| values[index] = valium_cast(value, columns[index], coders[index]) end end end |
#valium_select_one(attr_name) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/valium.rb', line 67 def valium_select_one(attr_name) column = columns_hash[attr_name] coder = serialized_attributes[attr_name] connection.select_rows( except(:select).select(arel_table[attr_name]).to_sql ).map! do |values| valium_cast(values[0], column, coder) end end |
#value_of(*attr_names) ⇒ Object Also known as: values_of
Minor version check
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/valium.rb', line 39 def value_of(*attr_names) attr_names.map! do |attr_name| attr_name = attr_name.to_s attr_name == 'id' ? primary_key : attr_name end if attr_names.size > 1 valium_select_multiple(attr_names) else valium_select_one(attr_names.first) end end |