preferable 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/preferable.rb +1 -0
- data/lib/preferable/coder.rb +29 -0
- data/lib/preferable/field.rb +1 -1
- data/lib/preferable/model.rb +7 -9
- data/lib/preferable/set.rb +13 -7
- metadata +9 -16
data/lib/preferable.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Preferable::Coder
|
2
|
+
|
3
|
+
def dump(obj)
|
4
|
+
ActiveSupport::JSON.encode(obj) if obj.is_a?(Hash) && obj.present?
|
5
|
+
end
|
6
|
+
|
7
|
+
def load(string)
|
8
|
+
return {} if string.blank?
|
9
|
+
return string if string.is_a?(Hash)
|
10
|
+
return legacy_load(string) if string.is_a?(String) && string =~ /^---/
|
11
|
+
|
12
|
+
begin
|
13
|
+
obj = ActiveSupport::JSON.decode(string) || {}
|
14
|
+
raise ActiveRecord::SerializationTypeMismatch, "Attribute was supposed to be a Hash, but was a #{obj.class}" unless obj.is_a?(Hash)
|
15
|
+
obj
|
16
|
+
rescue MultiJson::DecodeError
|
17
|
+
{}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def legacy_load(yaml)
|
24
|
+
lines = yaml.lines.to_a
|
25
|
+
lines.shift
|
26
|
+
YAML.load ["---", *lines].join("\n")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/preferable/field.rb
CHANGED
@@ -26,7 +26,7 @@ class Preferable::Field
|
|
26
26
|
def initialize(name, type, options = {})
|
27
27
|
raise ArgumentError, "Unknown type '#{type}', available types are: #{TYPES.map(&:to_s).join(', ')}" unless TYPES.include?(type.to_sym)
|
28
28
|
|
29
|
-
@name = name.
|
29
|
+
@name = name.to_s
|
30
30
|
@type = type.to_sym
|
31
31
|
@options = options.dup
|
32
32
|
@default = type_cast @options.delete(:default)
|
data/lib/preferable/model.rb
CHANGED
@@ -21,13 +21,12 @@ module Preferable::Model
|
|
21
21
|
# end
|
22
22
|
#
|
23
23
|
def preferable(&block)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
self._preferable.instance_eval(&block)
|
30
|
-
self._preferable
|
24
|
+
serialize :preferences, Preferable::Coder.new
|
25
|
+
include PreferableMethods
|
26
|
+
|
27
|
+
self._preferable = self._preferable.dup if self._preferable
|
28
|
+
self._preferable ||= Preferable::Schema.new
|
29
|
+
self._preferable.instance_eval(&block)
|
31
30
|
end
|
32
31
|
|
33
32
|
end
|
@@ -41,8 +40,7 @@ module Preferable::Model
|
|
41
40
|
# user.preferences[:theme_id] = 3
|
42
41
|
#
|
43
42
|
def preferences
|
44
|
-
|
45
|
-
value.is_a?(Preferable::Set) ? value : write_attribute(:preferences, Preferable::Set.new(self.class.name))
|
43
|
+
@preferences ||= Preferable::Set.wrap(self, super)
|
46
44
|
end
|
47
45
|
|
48
46
|
# Preferences writer. Updates existing preferences (doesn't replace them!)
|
data/lib/preferable/set.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
class Preferable::Set < Hash
|
2
|
-
MODEL_KEY = "_".freeze
|
3
2
|
PRIVATE = [:rehash, :fetch, :store, :shift, :delete, :delete_if, :keep_if].to_set.freeze
|
4
3
|
|
5
4
|
# Make destructive methods private
|
@@ -7,13 +6,15 @@ class Preferable::Set < Hash
|
|
7
6
|
PRIVATE.include?(m.to_sym) || m.to_s.ends_with?('!')
|
8
7
|
end
|
9
8
|
|
10
|
-
def
|
11
|
-
|
12
|
-
store MODEL_KEY, "::#{model_name}"
|
9
|
+
def self.wrap(owner, value)
|
10
|
+
new(owner).set(value || {})
|
13
11
|
end
|
14
12
|
|
15
|
-
|
16
|
-
|
13
|
+
attr_reader :owner
|
14
|
+
|
15
|
+
def initialize(owner)
|
16
|
+
super()
|
17
|
+
@owner = owner
|
17
18
|
end
|
18
19
|
|
19
20
|
def [](name)
|
@@ -30,6 +31,7 @@ class Preferable::Set < Hash
|
|
30
31
|
else
|
31
32
|
super field.name, value
|
32
33
|
end
|
34
|
+
owner.send :write_attribute, :preferences, to_hash
|
33
35
|
end
|
34
36
|
|
35
37
|
def set(pairs)
|
@@ -37,10 +39,14 @@ class Preferable::Set < Hash
|
|
37
39
|
self
|
38
40
|
end
|
39
41
|
|
42
|
+
def to_hash
|
43
|
+
{}.update(self)
|
44
|
+
end
|
45
|
+
|
40
46
|
private
|
41
47
|
|
42
48
|
def find_field(name)
|
43
|
-
|
49
|
+
owner.class._preferable[name.to_s] if owner
|
44
50
|
end
|
45
51
|
|
46
52
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preferable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-09-
|
19
|
+
date: 2011-09-30 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: abstract
|
@@ -38,22 +38,14 @@ dependencies:
|
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
44
|
-
segments:
|
45
|
-
- 3
|
46
|
-
- 0
|
47
|
-
- 0
|
48
|
-
version: 3.0.0
|
49
|
-
- - <
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
hash: 15
|
43
|
+
hash: 3
|
52
44
|
segments:
|
53
45
|
- 3
|
54
|
-
-
|
46
|
+
- 1
|
55
47
|
- 0
|
56
|
-
version: 3.
|
48
|
+
version: 3.1.0
|
57
49
|
type: :runtime
|
58
50
|
version_requirements: *id002
|
59
51
|
description: Great for storing user preferences
|
@@ -71,6 +63,7 @@ files:
|
|
71
63
|
- lib/preferable/model.rb
|
72
64
|
- lib/preferable/set.rb
|
73
65
|
- lib/preferable/schema.rb
|
66
|
+
- lib/preferable/coder.rb
|
74
67
|
homepage: https://github.com/bsm/preferable
|
75
68
|
licenses: []
|
76
69
|
|