marshalled_attributes 0.1.0
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.
- data/lib/marshalled_attributes.rb +48 -0
- metadata +55 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
module MarshalledAttributes
|
2
|
+
|
3
|
+
def MarshalledAttributes.enable_activerecord
|
4
|
+
return if ActiveRecord::Base.respond_to? :marshal_attr_accessor
|
5
|
+
ActiveRecord::Base.extend Associate
|
6
|
+
end
|
7
|
+
|
8
|
+
module Associate
|
9
|
+
def marshal_attr_accessor( mattrs, options )
|
10
|
+
raise MarshalledAttributesError, "Please specify the :as option (Should be a class constant, 'PartnerAttribute' - without quotes - for example)." if options[:wrapped_by].nil?
|
11
|
+
|
12
|
+
mattrs = [mattrs] unless mattrs.is_a? Array
|
13
|
+
key_column = options[:key_column] || :key_name
|
14
|
+
value_column = options[:value_column] || :value
|
15
|
+
attr_class = options[:wrapped_by]
|
16
|
+
|
17
|
+
mattrs.each do |mattr|
|
18
|
+
read_method_name = mattr.to_s
|
19
|
+
define_method( read_method_name ) do
|
20
|
+
ma = attr_class.find( :first,
|
21
|
+
:conditions => { "#{self.class.table_name.singularize}_id" => self.id,
|
22
|
+
key_column => read_method_name } )
|
23
|
+
return nil if ma.nil?
|
24
|
+
return Marshal.load( Base64.decode64(ma.value ) )
|
25
|
+
end
|
26
|
+
|
27
|
+
write_method_name = mattr.to_s + "="
|
28
|
+
define_method( write_method_name ) do |value|
|
29
|
+
marshalled = Base64.encode64( Marshal.dump( value ) )
|
30
|
+
|
31
|
+
ma = attr_class.find(:first,
|
32
|
+
:conditions => { "#{self.class.table_name.singularize}_id" => self.id,
|
33
|
+
key_column => read_method_name } )
|
34
|
+
ma = attr_class.create( key_column.to_sym => read_method_name,
|
35
|
+
"#{self.class.table_name.singularize}_id".to_sym => self.id ) if ma.nil?
|
36
|
+
ma.update_attribute( value_column.to_sym, marshalled )
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class MarshalledAttributesError < StandardError
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if defined? Rails
|
47
|
+
MarshalledAttributes::enable_activerecord if defined? ActiveRecord
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marshalled_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Nakashima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Adds k/v functionality through an ActiveRecord model in an association.
|
17
|
+
email: brettnak@gmail.com.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/marshalled_attributes.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Adds k/v functionality through an ActiveRecord model in an association.
|
54
|
+
test_files: []
|
55
|
+
|