serial_preference 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,42 +1,44 @@
|
|
1
|
-
|
1
|
+
module SerialPreference
|
2
|
+
class Preference
|
2
3
|
|
3
|
-
|
4
|
+
SUPPORTED_TYPES = [:string,:integer,:real,:float,:boolean,:password]
|
4
5
|
|
5
|
-
|
6
|
+
attr_accessor :data_type, :name, :default, :required, :label, :hint
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
def initialize(name,opts = {})
|
9
|
+
self.data_type = opts[:data_type] || :string
|
10
|
+
self.name = name
|
11
|
+
self.default = opts[:default]
|
12
|
+
self.required = !!opts[:required]
|
13
|
+
self.label = opts[:label]
|
14
|
+
self.hint = opts[:hint]
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def required?
|
18
|
+
required
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def numerical?
|
22
|
+
[:integer,:float,:real].include?(data_type)
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
25
|
+
def value(value)
|
26
|
+
value = value.presence || default
|
27
|
+
if !value.nil?
|
28
|
+
case data_type
|
29
|
+
when :string, :password
|
30
|
+
value.to_s
|
31
|
+
when :integer
|
32
|
+
value.to_i
|
33
|
+
when :float, :real
|
34
|
+
value.to_f
|
35
|
+
when :boolean
|
36
|
+
!!value
|
37
|
+
else
|
38
|
+
nil
|
39
|
+
end
|
38
40
|
end
|
39
41
|
end
|
40
|
-
end
|
41
42
|
|
42
|
-
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,41 +1,43 @@
|
|
1
|
-
|
1
|
+
module SerialPreference
|
2
|
+
class PreferenceGroup
|
2
3
|
|
3
|
-
|
4
|
+
attr_accessor :name, :label
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def initialize(name, opts = {})
|
7
|
+
@preferences = {}
|
8
|
+
@name = name
|
9
|
+
@label = opts[:label]
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def label
|
13
|
+
@label.presence || name.to_s.titleize
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def preference_keys
|
17
|
+
@preferences.keys
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def preferences
|
21
|
+
@preferences.values
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def pref(name,opts = {})
|
25
|
+
@preferences[name] = Preference.new(name,opts)
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
SerialPreference::Preference::SUPPORTED_TYPES.each do |dt|
|
29
|
+
define_method dt do |name,opts = {}|
|
30
|
+
pref(name,opts.merge!(:data_type => dt))
|
31
|
+
end
|
30
32
|
end
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def respond_to?(name,*opts)
|
35
|
+
@preferences[name].present?
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
def method_missing(name,*opts,&block)
|
39
|
+
pref(name,*opts)
|
40
|
+
end
|
40
41
|
|
41
|
-
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/serial_preference.rb
CHANGED
@@ -34,7 +34,7 @@ module SerialPreference
|
|
34
34
|
|
35
35
|
def preference_group(name, opts={},&block)
|
36
36
|
self.preference_groups[current_context] ||= {}
|
37
|
-
self.preference_groups[current_context][name] ||= PreferenceGroup.new(name,opts)
|
37
|
+
self.preference_groups[current_context][name] ||= SerialPreference::PreferenceGroup.new(name,opts)
|
38
38
|
self.preference_groups[current_context][name].instance_exec(&block)
|
39
39
|
end
|
40
40
|
|
@@ -83,7 +83,7 @@ module SerialPreference
|
|
83
83
|
|
84
84
|
def base_group
|
85
85
|
@base_group ||= begin
|
86
|
-
pg = PreferenceGroup.new(:base,:label => current_context.to_s.titleize)
|
86
|
+
pg = SerialPreference::PreferenceGroup.new(:base,:label => current_context.to_s.titleize)
|
87
87
|
self.preference_groups[current_context] ||= {}
|
88
88
|
self.preference_groups[current_context][:base] = pg
|
89
89
|
pg
|