serialized_attr_accessors 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/serialized_attr_accessors.rb +46 -0
- metadata +47 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
module SerializedAttrAccessors
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
|
5
|
+
def unserialized_options
|
6
|
+
@so_hash ||= (self.send(self.class.send(:get_serialized_attr)) || {})
|
7
|
+
end
|
8
|
+
|
9
|
+
def populate_serialized_options
|
10
|
+
self.send("#{self.class.send(:get_serialized_attr)}=", @so_hash) if @so_hash
|
11
|
+
end
|
12
|
+
|
13
|
+
base.send(:before_validation, :populate_serialized_options)
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
#Sets serialized attribute, Default serialized attribute name is "serialized_options" until and unless set manually
|
18
|
+
def set_serialized_attribute(attribute_name)
|
19
|
+
@ser_attr = attribute_name
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_serialized_attr
|
23
|
+
@ser_attr ||= "serialized_options"
|
24
|
+
end
|
25
|
+
|
26
|
+
#Generates getter and setter method with field_name and default_value (if provided else nil)
|
27
|
+
#Example:
|
28
|
+
# sattr_accessor :second_name, "kumar"
|
29
|
+
# sattr_accessor :address
|
30
|
+
def sattr_accessor(filed_name, default_value = nil)
|
31
|
+
|
32
|
+
define_method filed_name do
|
33
|
+
(unserialized_options[filed_name.to_sym] || default_value)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method "#{filed_name}=" do |field_value|
|
37
|
+
unserialized_options.merge!(filed_name.to_sym => field_value)
|
38
|
+
field_value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
ActiveRecord::Base.send(:include, SerializedAttrAccessors)
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: serialized_attr_accessors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Praveen Kumar Sinha
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-23 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'SerializedAttrAccessors for generating attr_accessors backed by serialized
|
15
|
+
column using method sattr_accessor '
|
16
|
+
email: praveen.kumar.sinha@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/serialized_attr_accessors.rb
|
22
|
+
homepage: https://github.com/praveenkumarsinha/SerializedAttributes
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.10
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: SerializedAttrAccessors for generating attr_accessors backed by serialized
|
46
|
+
column.
|
47
|
+
test_files: []
|