serial_preference 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,9 +33,10 @@ Or install it yourself as:
33
33
 
34
34
  ## Usage
35
35
 
36
+ ````ruby
36
37
  class Company < ActiveRecord::Base
37
38
 
38
- include SerialPreference::Preferencability # will change this
39
+ include HasPreferenceMap
39
40
 
40
41
  preference_map :preferences do
41
42
 
@@ -65,6 +66,7 @@ Or install it yourself as:
65
66
  end
66
67
 
67
68
  end
69
+ ````
68
70
 
69
71
  ## Contributing
70
72
 
@@ -0,0 +1,69 @@
1
+ module HasPreferenceMap
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ class_attribute :preference_context
6
+ class_attribute :preference_storage_attribute
7
+ self.preference_context = table_name.downcase.to_sym
8
+ self.preference_storage_attribute = :preferences
9
+ end
10
+
11
+ def default_preference_for(name,context = nil)
12
+ self.class.default_preference_for(name,context)
13
+ end
14
+
15
+ def preferences_for(group_name,storage_accessor = nil, context = nil)
16
+ send(storage_accessor || preference_storage_attribute).slice(*self.class.preferences_for(group_name,context))
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def preferences_for(group_name,context = nil)
22
+ SerialPreference::Preferenzer.group_for(context || preference_context)[group_name].try(:preference_keys) || []
23
+ end
24
+
25
+ def preference_map(store_accessor = :preferences, context = nil, &block)
26
+ pc = context || preference_context
27
+ sa = store_accessor || self.preference_storage_attribute
28
+ SerialPreference::Preferenzer.draw(pc,&block)
29
+ prefers(sa,pc)
30
+ end
31
+
32
+ def default_preference_for(name,context = nil)
33
+ SerialPreference::Preferenzer.preference(name,nil,context || preference_context)
34
+ end
35
+
36
+ def prefers(store_accessor = :preferences, context = nil)
37
+ pc = context || preference_context
38
+ sa = store_accessor || self.preference_storage_attribute
39
+
40
+ preferences = SerialPreference::Preferenzer.preferences_for(pc)
41
+
42
+ serialize sa, Hash
43
+
44
+ preferences.each do |preference|
45
+ key = preference.name
46
+ define_method("#{key}=") do |value|
47
+ send(store_accessor)[key] = SerialPreference::Preferenzer.preference(key,value,context)
48
+ send("#{sa}_will_change!")
49
+ end
50
+
51
+ define_method(key) do
52
+ SerialPreference::Preferenzer.preference(key,send(sa)[key],context)
53
+ end
54
+
55
+ if preference.required?
56
+ validates key, :presence => true
57
+ end
58
+
59
+ if preference.numerical?
60
+ validates key, :numericality => true, :allow_blank => true
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -1,3 +1,3 @@
1
1
  module SerialPreference
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serial_preference
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -24,7 +24,7 @@ files:
24
24
  - README.md
25
25
  - Rakefile
26
26
  - lib/serial_preference.rb
27
- - lib/serial_preference/preferencability.rb
27
+ - lib/serial_preference/has_preference_map.rb
28
28
  - lib/serial_preference/preference.rb
29
29
  - lib/serial_preference/preference_group.rb
30
30
  - lib/serial_preference/version.rb
@@ -1,67 +0,0 @@
1
- module Preferencability
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- class_attribute :preference_context
6
- class_attribute :preference_storage_attribute
7
- self.preference_context = table_name.to_sym
8
- self.preference_storage_attribute = :preferences
9
- end
10
-
11
- def default_preference_for(name)
12
- self.class.default_preference_for(name)
13
- end
14
-
15
- def preferences_for(group_name)
16
- send(preference_storage_attribute).slice(*self.class.preferences_for(group_name))
17
- end
18
-
19
- module ClassMethods
20
-
21
- def preferences_for(group_name)
22
- SerialPreference::Preferenzer.group_for(self.preference_context)[group_name].try(:preference_keys) || []
23
- end
24
-
25
- def preference_map(store_accessor = :preferences, context = nil, &block)
26
- self.preference_context = context || self.preference_context
27
- SerialPreference::Preferenzer.draw(preference_context,&block)
28
- prefers(store_accessor,preference_context)
29
- end
30
-
31
- def default_preference_for(name)
32
- SerialPreference::Preferenzer.preference(name,nil,preference_context)
33
- end
34
-
35
- def prefers(store_accessor = :preferences, context = nil)
36
- self.preference_context = context || self.preference_context
37
- self.preference_storage_attribute = store_accessor || self.preference_storage_attribute
38
- serialize preference_storage_attribute, Hash
39
-
40
- preferences = SerialPreference::Preferenzer.preferences_for(preference_context)
41
-
42
- preferences.each do |preference|
43
- key = preference.name
44
- define_method("#{key}=") do |value|
45
- send(:preferences)[key] = SerialPreference::Preferenzer.preference(key,value,context)
46
- send("preferences_will_change!")
47
- end
48
-
49
- define_method(key) do
50
- SerialPreference::Preferenzer.preference(key,send(:preferences)[key],context)
51
- end
52
-
53
- if preference.required?
54
- validates key, :presence => true
55
- end
56
-
57
- if preference.numerical?
58
- validates key, :numericality => true, :allow_blank => true
59
- end
60
-
61
- end
62
-
63
- end
64
-
65
- end
66
-
67
- end