serial_preference 0.1.2 → 0.2.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/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 amitsuroliya
1
+ Copyright (c) 2012 Aditya Sanghi
2
2
 
3
3
  MIT License
4
4
 
@@ -4,64 +4,73 @@ module HasPreferenceMap
4
4
  included do
5
5
  class_attribute :preference_context
6
6
  class_attribute :preference_storage_attribute
7
- self.preference_context = table_name.downcase.to_sym
8
7
  self.preference_storage_attribute = :preferences
8
+ self.preference_context = "#{preference_storage_attribute}_#{table_name.downcase}".to_sym
9
9
  end
10
10
 
11
- def default_preference_for(name,context = nil)
12
- self.class.default_preference_for(name,context)
11
+ def default_preference_for(name)
12
+ self.class.default_preference_for(name)
13
13
  end
14
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))
15
+ def preferences_for(group_name)
16
+ send(preference_storage_attribute).slice(*self.class.preferences_for(group_name))
17
+ end
18
+
19
+ def preference_map
20
+ SerialPreference::Preferenzer.preferences_for(self.class.preference_context)
17
21
  end
18
22
 
19
23
  module ClassMethods
20
24
 
21
- def preferences_for(group_name,context = nil)
22
- SerialPreference::Preferenzer.group_for(context || preference_context)[group_name].try(:preference_keys) || []
25
+ def preference_groups
26
+ SerialPreference::Preferenzer.group_for(preference_context).try(:values) || []
23
27
  end
24
28
 
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)
29
+ def preferences_for(group_name)
30
+ SerialPreference::Preferenzer.group_for(preference_context)[group_name].try(:preference_keys) || []
30
31
  end
31
32
 
32
- def default_preference_for(name,context = nil)
33
- SerialPreference::Preferenzer.preference(name,nil,context || preference_context)
33
+ def preference_map(store_attribute = nil, &block)
34
+ self.preference_storage_attribute = store_attribute || preference_storage_attribute
35
+ self.preference_context = "#{preference_storage_attribute}_#{table_name.downcase}".to_sym
36
+ SerialPreference::Preferenzer.reset(preference_context)
37
+ preferences = SerialPreference::Preferenzer.draw(preference_context,&block)
38
+
39
+ make_functions(preferences,store_attribute,preference_context)
34
40
  end
35
41
 
36
- def prefers(store_accessor = :preferences, context = nil)
37
- pc = context || preference_context
38
- sa = store_accessor || self.preference_storage_attribute
42
+ def default_preference_for(name)
43
+ SerialPreference::Preferenzer.preference(name,nil,preference_context)
44
+ end
39
45
 
40
- preferences = SerialPreference::Preferenzer.preferences_for(pc)
46
+ private
41
47
 
42
- serialize sa, Hash
48
+ def make_functions(preferences,store_attribute,context)
49
+ serialize store_attribute, Hash
43
50
 
44
51
  preferences.each do |preference|
45
52
  key = preference.name
46
53
  define_method("#{key}=") do |value|
47
- send(store_accessor)[key] = SerialPreference::Preferenzer.preference(key,value,context)
48
- send("#{sa}_will_change!")
54
+ send(store_attribute)[key] = SerialPreference::Preferenzer.preference(key,value,context)
55
+ send("#{store_attribute}_will_change!")
49
56
  end
50
57
 
51
58
  define_method(key) do
52
- SerialPreference::Preferenzer.preference(key,send(sa)[key],context)
59
+ value = send(store_attribute)[key]
60
+ SerialPreference::Preferenzer.preference(key,value,context)
53
61
  end
54
62
 
63
+ opts = {}
55
64
  if preference.required?
56
- validates key, :presence => true
65
+ opts[:presence] = true
57
66
  end
58
-
59
67
  if preference.numerical?
60
- validates key, :numericality => true, :allow_blank => true
68
+ opts[:numericality] = true
69
+ opts[:allow_blank] = true unless opts[:presence].present?
61
70
  end
71
+ validates key, opts if opts.present?
62
72
 
63
73
  end
64
-
65
74
  end
66
75
 
67
76
  end
@@ -3,7 +3,7 @@ module SerialPreference
3
3
 
4
4
  SUPPORTED_TYPES = [:string,:integer,:real,:float,:boolean,:password]
5
5
 
6
- attr_accessor :data_type, :name, :default, :required, :label, :hint
6
+ attr_accessor :data_type, :name, :default, :required, :label, :hint, :field_type
7
7
 
8
8
  def initialize(name,opts = {})
9
9
  self.data_type = opts[:data_type] || :string
@@ -12,6 +12,7 @@ module SerialPreference
12
12
  self.required = !!opts[:required]
13
13
  self.label = opts[:label]
14
14
  self.hint = opts[:hint]
15
+ self.field_type = opts[:field_type]
15
16
  end
16
17
 
17
18
  def required?
@@ -22,8 +23,12 @@ module SerialPreference
22
23
  [:integer,:float,:real].include?(data_type)
23
24
  end
24
25
 
26
+ def field_type
27
+ @field_type || (numerical? ? :string : data_type)
28
+ end
29
+
25
30
  def value(value)
26
- value = value.presence || default
31
+ value = value.nil? ? default : value
27
32
  if !value.nil?
28
33
  case data_type
29
34
  when :string, :password
@@ -9,6 +9,14 @@ module SerialPreference
9
9
  @label = opts[:label]
10
10
  end
11
11
 
12
+ def to_s
13
+ name.to_s
14
+ end
15
+
16
+ def titleize
17
+ label
18
+ end
19
+
12
20
  def label
13
21
  @label.presence || name.to_s.titleize
14
22
  end
@@ -1,3 +1,3 @@
1
1
  module SerialPreference
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -5,19 +5,20 @@ require 'serial_preference/preference_group'
5
5
  require "serial_preference/has_preference_map"
6
6
 
7
7
  module SerialPreference
8
- class Preferenzer
8
+ class Preferenzer
9
9
 
10
- include Singleton
10
+ include Singleton
11
11
 
12
- attr_accessor :preference_groups, :current_context
12
+ attr_accessor :preference_groups, :current_context
13
13
 
14
- def self.draw(context = :base, &block)
15
- i = instance
16
- i.preference_groups ||= {}
17
- i.preference_groups[context] ||= {}
18
- i.current_context = context
19
- i.instance_exec(&block)
20
- end
14
+ def self.draw(context = :base, &block)
15
+ i = instance
16
+ i.preference_groups ||= {}
17
+ i.preference_groups[context] ||= {}
18
+ i.current_context = context
19
+ i.instance_exec(&block)
20
+ i.preferences_for(context)
21
+ end
21
22
 
22
23
  def +(name,opts = {})
23
24
  preference(name,opts)
@@ -27,69 +28,73 @@ module SerialPreference
27
28
  preference_group(name,opts,&block)
28
29
  end
29
30
 
30
- def preference(name,opts = {})
31
- pg = base_group
32
- pg.pref(name,opts)
33
- end
34
-
35
- def preference_group(name, opts={},&block)
36
- self.preference_groups[current_context] ||= {}
37
- self.preference_groups[current_context][name] ||= SerialPreference::PreferenceGroup.new(name,opts)
38
- self.preference_groups[current_context][name].instance_exec(&block)
39
- end
40
-
41
- def self.method_missing(name,*args,&block)
42
- if instance.respond_to?(name)
43
- instance.send(name,*args,&block)
44
- else
45
- super
46
- end
47
- end
48
-
49
- def all_preference_names(context = :base)
50
- group_for(context).values.map do |pg|
51
- pg.preferences.map do |pref|
52
- pref.name
53
- end
54
- end.flatten
55
- end
56
-
57
- def preferences_for(context = :base)
58
- group_for(context).values.map do |pg|
59
- pg.preferences.map do |pref|
60
- pref
61
- end
62
- end.flatten
63
- end
64
-
65
- def each_preference(context = :base)
66
- group_for(context).values.each do |pg|
67
- pg.preferences.each do |pref|
68
- yield(pref)
69
- end
70
- end
71
- end
72
-
73
- def preference(name,value,context = :base)
74
- p = preferences_for(context).find{|x| x.name == name }
75
- p ? p.value(value) : nil
76
- end
77
-
78
- def group_for(context = :base)
79
- preference_groups[context] || {}
80
- end
81
-
82
- private
83
-
84
- def base_group
85
- @base_group ||= begin
86
- pg = SerialPreference::PreferenceGroup.new(:base,:label => current_context.to_s.titleize)
87
- self.preference_groups[current_context] ||= {}
88
- self.preference_groups[current_context][:base] = pg
89
- pg
90
- end
91
- end
92
-
93
- end
31
+ def reset(context = :base)
32
+ self.preference_groups.try(:delete,context)
33
+ end
34
+
35
+ def preference(name,opts = {})
36
+ pg = base_group
37
+ pg.pref(name,opts)
38
+ end
39
+
40
+ def preference_group(name, opts={},&block)
41
+ self.preference_groups[current_context] ||= {}
42
+ self.preference_groups[current_context][name] ||= SerialPreference::PreferenceGroup.new(name,opts)
43
+ self.preference_groups[current_context][name].instance_exec(&block)
44
+ end
45
+
46
+ def self.method_missing(name,*args,&block)
47
+ if instance.respond_to?(name)
48
+ instance.send(name,*args,&block)
49
+ else
50
+ super
51
+ end
52
+ end
53
+
54
+ def all_preference_names(context = :base)
55
+ group_for(context).values.map do |pg|
56
+ pg.preferences.map do |pref|
57
+ pref.name
58
+ end
59
+ end.flatten
60
+ end
61
+
62
+ def preferences_for(context = :base)
63
+ group_for(context).values.map do |pg|
64
+ pg.preferences.map do |pref|
65
+ pref
66
+ end
67
+ end.flatten
68
+ end
69
+
70
+ def each_preference(context = :base)
71
+ group_for(context).values.each do |pg|
72
+ pg.preferences.each do |pref|
73
+ yield(pref)
74
+ end
75
+ end
76
+ end
77
+
78
+ def preference(name,value,context = :base)
79
+ p = preferences_for(context).find{|x| x.name == name }
80
+ p ? p.value(value) : nil
81
+ end
82
+
83
+ def group_for(context = :base)
84
+ preference_groups[context] || {}
85
+ end
86
+
87
+ private
88
+
89
+ def base_group
90
+ @base_group ||= begin
91
+ pg = SerialPreference::PreferenceGroup.new(:base,:label => current_context.to_s.titleize)
92
+ self.preference_groups[current_context] ||= {}
93
+ self.preference_groups[current_context][:base] = pg
94
+ pg
95
+ end
96
+ end
97
+
98
+ end
94
99
  end
95
100
 
@@ -16,4 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "activesupport", ">= 3.0.0"
21
+
19
22
  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.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-09-09 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
14
30
  description: Serialized Preferences for your models
15
31
  email:
16
32
  - aditya.sanghi@risingsuntech.net