serialist 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,79 @@
1
+ # ActsAsSerializable
2
+ module Serialist
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ attr_accessor :serialist_options
10
+ attr_accessor :serialist_field
11
+
12
+ def serialist(serialist_field, serialist_options = [])
13
+
14
+
15
+ @serialist_field ||= serialist_field
16
+ @serialist_options ||= []
17
+ @serialist_options = (@serialist_options + serialist_options).uniq
18
+ serialize(@serialist_field, Hash)
19
+
20
+ if serialist_options.empty?
21
+ include Serialist::InstanceMethods
22
+ else
23
+ @serialist_options.each do |field|
24
+ define_method field.to_s do
25
+ return nil unless (slug = self.send(serialist_field))
26
+ slug[field]
27
+ end
28
+ define_method field.to_s + "?" do |*param|
29
+ return false unless (slug = self.send(serialist_field))
30
+ if param.empty?
31
+ ![nil, false, "false", :false].include?(slug[field])
32
+ else
33
+ slug[field] == param.first
34
+ end
35
+ end
36
+ define_method field.to_s + "=" do |param|
37
+ update_attribute(serialist_field, Hash.new) unless self.send(serialist_field)
38
+ self.send(serialist_field)[field] = param
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def inherited(subclass)
45
+ super
46
+ subclass.instance_variable_set("@serialist_field", @serialist_field)
47
+ subclass.instance_variable_set("@serialist_options", @serialist_options)
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ def method_missing(method, *args, &block)
53
+ begin
54
+ super
55
+ rescue NoMethodError
56
+ slug = self.send(self.class.serialist_field)
57
+
58
+ case method.to_s.last
59
+ when "?"
60
+ slug && slug[method.to_s[0..-2].to_sym] == (args && args.first || "true")
61
+
62
+ if args.empty?
63
+ slug && ![nil, false, "false", :false].include?(slug[method.to_s[0..-2].to_sym])
64
+ else
65
+ slug && (slug[method.to_s[0..-2].to_sym] == args.first)
66
+ end
67
+
68
+ when "="
69
+ update_attribute(self.class.serialist_field, Hash.new) unless slug
70
+ self.send(self.class.serialist_field)[method.to_s[0..-2].to_sym] = args.first
71
+ else
72
+ slug && slug[method]
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+
data/serialist.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{serialist}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benoit B\303\251n\303\251zech"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "install.rb",
29
29
  "installation-template.txt",
30
30
  "lib/serialist.rb",
31
+ "lib/serialist/serialist_module.rb",
31
32
  "rails/init.rb",
32
33
  "serialist.gemspec",
33
34
  "tasks/acts_as_serializable_tasks.rake",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Benoit B\xC3\xA9n\xC3\xA9zech"
@@ -34,6 +34,7 @@ files:
34
34
  - install.rb
35
35
  - installation-template.txt
36
36
  - lib/serialist.rb
37
+ - lib/serialist/serialist_module.rb
37
38
  - rails/init.rb
38
39
  - serialist.gemspec
39
40
  - tasks/acts_as_serializable_tasks.rake