serialized_attr_accessors 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/serialized_attr_accessors.rb +52 -16
  3. metadata +10 -11
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1828d766de661b559f30e12f60c1622c17c3d95
4
+ data.tar.gz: 88ebe82796acfd06727200c1902ab13c98945a2e
5
+ SHA512:
6
+ metadata.gz: db937425d71b600597180f20c31e35a48562873bfdac7b9faf7214eeaee8499921981f60a305d0279a35e47cce7d871d71ab1fcf7ba450fe3ef283e0070670c7
7
+ data.tar.gz: 85770f7cb3c308b795498d8ade3828592f31ac484eb9e12f8d32eeba2af583d7dc07b9e0ac990fbfa077f97ba9211f3ebb860e7e2c490597a027a9c25082e990
@@ -2,45 +2,81 @@ module SerializedAttrAccessors
2
2
  def self.included(base)
3
3
  base.extend(ClassMethods)
4
4
 
5
- def unserialized_options
6
- @so_hash ||= (self.send(self.class.send(:get_serialized_attr)) || {})
5
+ def unserialized_options(serialized_attr)
6
+ @so_hash ||= {}
7
+ (@so_hash[serialized_attr] ||= (self.send(serialized_attr) || {}))
7
8
  end
8
9
 
9
- def populate_serialized_options
10
- self.send("#{self.class.send(:get_serialized_attr)}=", @so_hash) if @so_hash
10
+ def populate_serialized_attributes
11
+ self.class.serialized_attribute_list.each do |parent_attr_key, value|
12
+ self.send("#{parent_attr_key.to_s}=", @so_hash[parent_attr_key]) if @so_hash and @so_hash[parent_attr_key]
13
+ end
11
14
  end
12
15
 
13
- base.send(:before_validation, :populate_serialized_options)
16
+ base.send(:before_validation, :populate_serialized_attributes)
14
17
  end
15
18
 
16
19
  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
+ #Stores list of attributes serialized
21
+ def serialized_attribute_list
22
+ @parent_attribute_list ||= {:serialized_options => []}
20
23
  end
21
24
 
22
- def get_serialized_attr
23
- @ser_attr ||= "serialized_options"
25
+ #Gets serialized attribute currenly in use
26
+ def current_serialized_attr
27
+ @curr_ser_attr ||= serialized_attribute_list.keys.first
24
28
  end
25
29
 
26
30
  #Generates getter and setter method with field_name and default_value (if provided else nil)
27
31
  #Example:
28
32
  # sattr_accessor :second_name, "kumar"
29
33
  # sattr_accessor :address
30
- def sattr_accessor(filed_name, default_value = nil)
34
+ def sattr_accessor(field_name, default_value = nil)
35
+ field_name = field_name.to_sym unless field_name.is_a?(Symbol)
36
+
37
+ serialized_attribute_list[current_serialized_attr] ||= []
38
+ serialized_attribute_list[current_serialized_attr] << field_name
39
+
40
+ #If attributes are not serialized then here is serialization done
41
+ self.serialize(current_serialized_attr) unless self.serialized_attributes.keys.include?(current_serialized_attr.to_s)
42
+
43
+ #Defining method to fetch serialzed parent attribute (gives last found)
44
+ define_method :fetch_parent_attribute do |filed_name|
45
+ parent_attr = nil
46
+ self.class.serialized_attribute_list.each do |attr_key, fields_val|
47
+ parent_attr = attr_key if fields_val.include?(filed_name)
48
+ end
49
+ raise "UnableToFindParentAttribute" if parent_attr.nil?
50
+ parent_attr
51
+ end
31
52
 
32
- define_method filed_name do
33
- (unserialized_options[filed_name.to_sym] || default_value)
53
+ define_method field_name do
54
+ (unserialized_options(fetch_parent_attribute(field_name))[field_name] || default_value)
34
55
  end
35
56
 
36
- define_method "#{filed_name}=" do |field_value|
37
- unserialized_options.merge!(filed_name.to_sym => field_value)
57
+ define_method "#{field_name.to_s}=" do |field_value|
58
+ unserialized_options(fetch_parent_attribute(field_name)).merge!(field_name => field_value)
38
59
  field_value
39
60
  end
40
61
  end
41
62
 
63
+ #for_serialized_field :workspaces do
64
+ # sattr_accessor(:wfield_one, "Some String One")
65
+ # sattr_accessor(:wfield_two, {})
66
+ # sattr_accessor(:wfield_three, [])
67
+ #end
68
+ def for_serialized_field(fieldname)
69
+ if block_given?
70
+ @curr_ser_attr = fieldname
71
+ yield
72
+ @curr_ser_attr = serialized_attribute_list.keys.first
73
+ else
74
+ raise "ExpectedBlockWithAttributes"
75
+ end
76
+ end
77
+
42
78
  end
43
79
 
44
80
  end
45
81
 
46
- ActiveRecord::Base.send(:include, SerializedAttrAccessors)
82
+ ActiveRecord::Base.send(:include, SerializedAttrAccessors)
metadata CHANGED
@@ -1,18 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialized_attr_accessors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Praveen Kumar Sinha
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000Z
11
+ date: 2013-04-15 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ! 'SerializedAttrAccessors for generating attr_accessors backed by serialized
15
- column using method sattr_accessor '
13
+ description: attribute accessor generator using sattr_accessor (with default field
14
+ :serialized_options) or a different serialized field using for_serialized_field
15
+ (with block)
16
16
  email:
17
17
  - praveen.kumar.sinha@gmail.com
18
18
  executables: []
@@ -22,27 +22,26 @@ files:
22
22
  - lib/serialized_attr_accessors.rb
23
23
  homepage: https://github.com/praveenkumarsinha/SerializedAttributes/
24
24
  licenses: []
25
+ metadata: {}
25
26
  post_install_message:
26
27
  rdoc_options: []
27
28
  require_paths:
28
29
  - lib
29
30
  required_ruby_version: !ruby/object:Gem::Requirement
30
- none: false
31
31
  requirements:
32
- - - ! '>='
32
+ - - '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  required_rubygems_version: !ruby/object:Gem::Requirement
36
- none: false
37
36
  requirements:
38
- - - ! '>='
37
+ - - '>='
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
41
40
  requirements: []
42
41
  rubyforge_project:
43
- rubygems_version: 1.8.10
42
+ rubygems_version: 2.0.0.rc.2
44
43
  signing_key:
45
- specification_version: 3
44
+ specification_version: 4
46
45
  summary: SerializedAttrAccessors for generating attr_accessors backed by serialized
47
46
  column.
48
47
  test_files: []