property_sets 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -34,6 +34,9 @@ account.settings.featured?
34
34
 
35
35
  # Short hand for setting one or more values
36
36
  account.settings.set(:version => "v1.2", :activated => true)
37
+
38
+ # Short hand for getting a hash with pairs for each key argument
39
+ account.settings.get([:version, :activated])
37
40
  ```
38
41
 
39
42
  ### Validations
@@ -20,6 +20,21 @@ module PropertySets
20
20
 
21
21
  hash_opts = {:class_name => property_class.name, :autosave => true, :dependent => :destroy}.merge(options)
22
22
  has_many association, hash_opts do
23
+ # Accepts an array of names as strings or symbols and returns a hash.
24
+ def get(keys = [])
25
+ property_keys = if keys.empty?
26
+ association_class.keys
27
+ else
28
+ association_class.keys & keys.map(&:to_s)
29
+ end
30
+
31
+ property_pairs = property_keys.map do |name|
32
+ value = lookup_value(association_class.type(name), name)
33
+ [name.to_s, value]
34
+ end.flatten
35
+ HashWithIndifferentAccess[*property_pairs]
36
+ end
37
+
23
38
  # Accepts a name value pair hash { :name => 'value', :pairs => true } and builds a property for each key
24
39
  def set(property_pairs, with_protection = false)
25
40
  property_pairs.keys.each do |name|
@@ -145,17 +160,20 @@ module PropertySets
145
160
  # This finder method returns the property if present, otherwise a new instance with the default value.
146
161
  # It does not have the side effect of adding a new setting object.
147
162
  def lookup_or_default(arg)
148
- instance = lookup_without_default(arg)
149
- instance ||= begin
163
+ instance = lookup_without_default(arg)
164
+ instance ||= association_class.new(:value => raw_default(arg))
165
+ instance.value_serialized = property_serialized?(arg)
166
+ instance
167
+ end
168
+
169
+ def association_class
170
+ @association_class ||= begin
150
171
  if ActiveRecord::VERSION::STRING >= "3.1.0"
151
- association_class = proxy_association.klass
172
+ proxy_association.klass
152
173
  else
153
- association_class = @reflection.klass
174
+ @reflection.klass
154
175
  end
155
- association_class.new(:value => raw_default(arg))
156
176
  end
157
- instance.value_serialized = property_serialized?(arg)
158
- instance
159
177
  end
160
178
  end
161
179
  end
@@ -1,3 +1,3 @@
1
1
  module PropertySets
2
- VERSION = "2.1.0"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -145,6 +145,39 @@ class TestPropertySets < ActiveSupport::TestCase
145
145
  end
146
146
  end
147
147
 
148
+ context "#get" do
149
+ setup { @account.settings.set(:baz => "456") }
150
+
151
+ should "fetch property pairs with string arguments" do
152
+ assert @account.settings.lookup_without_default(:baz)
153
+ assert_equal({"baz" => "456"}, @account.settings.get(["baz"]))
154
+ end
155
+
156
+ should "fetch property pairs with symbol arguments" do
157
+ assert_equal({"baz" => "456"}, @account.settings.get([:baz]))
158
+ end
159
+
160
+ should "return all property pairs if no arguments are provided" do
161
+ assert_same_elements(
162
+ ["foo", "bar", "baz", "hep", "pro"],
163
+ @account.settings.get.keys
164
+ )
165
+ end
166
+
167
+ should "ignore non-existent keys" do
168
+ assert_equal({"baz" => "456"}, @account.settings.get([:baz, :red]))
169
+ end
170
+
171
+ should "include default property pairs" do
172
+ assert_nil @account.settings.lookup_without_default(:hep)
173
+ assert_equal({"hep" => "skep"}, @account.settings.get(["hep"]))
174
+ end
175
+
176
+ should "return a hash with values that can be fetched by string or symbol" do
177
+ assert_equal "456", @account.settings.get(["baz"]).fetch(:baz)
178
+ end
179
+ end
180
+
148
181
  context "#set" do
149
182
  should "support writing multiple values to the association" do
150
183
  assert !@account.settings.foo?
metadata CHANGED
@@ -1,20 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: property_sets
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Morten Primdahl
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-11-12 00:00:00.000000000 Z
12
+ date: 2013-11-20 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: activesupport
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: 2.3.14
20
22
  - - <
@@ -23,8 +25,9 @@ dependencies:
23
25
  type: :runtime
24
26
  prerelease: false
25
27
  version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
26
29
  requirements:
27
- - - '>='
30
+ - - ! '>='
28
31
  - !ruby/object:Gem::Version
29
32
  version: 2.3.14
30
33
  - - <
@@ -33,8 +36,9 @@ dependencies:
33
36
  - !ruby/object:Gem::Dependency
34
37
  name: activerecord
35
38
  requirement: !ruby/object:Gem::Requirement
39
+ none: false
36
40
  requirements:
37
- - - '>='
41
+ - - ! '>='
38
42
  - !ruby/object:Gem::Version
39
43
  version: 2.3.14
40
44
  - - <
@@ -43,8 +47,9 @@ dependencies:
43
47
  type: :runtime
44
48
  prerelease: false
45
49
  version_requirements: !ruby/object:Gem::Requirement
50
+ none: false
46
51
  requirements:
47
- - - '>='
52
+ - - ! '>='
48
53
  - !ruby/object:Gem::Version
49
54
  version: 2.3.14
50
55
  - - <
@@ -53,15 +58,17 @@ dependencies:
53
58
  - !ruby/object:Gem::Dependency
54
59
  name: json
55
60
  requirement: !ruby/object:Gem::Requirement
61
+ none: false
56
62
  requirements:
57
- - - '>='
63
+ - - ! '>='
58
64
  - !ruby/object:Gem::Version
59
65
  version: '0'
60
66
  type: :runtime
61
67
  prerelease: false
62
68
  version_requirements: !ruby/object:Gem::Requirement
69
+ none: false
63
70
  requirements:
64
- - - '>='
71
+ - - ! '>='
65
72
  - !ruby/object:Gem::Version
66
73
  version: '0'
67
74
  description: This gem is an ActiveRecord extension which provides a convenient interface
@@ -101,25 +108,26 @@ files:
101
108
  homepage: http://github.com/zendesk/property_sets
102
109
  licenses:
103
110
  - MIT
104
- metadata: {}
105
111
  post_install_message:
106
112
  rdoc_options: []
107
113
  require_paths:
108
114
  - lib
109
115
  required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
110
117
  requirements:
111
- - - '>='
118
+ - - ! '>='
112
119
  - !ruby/object:Gem::Version
113
120
  version: '0'
114
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
115
123
  requirements:
116
- - - '>='
124
+ - - ! '>='
117
125
  - !ruby/object:Gem::Version
118
126
  version: '0'
119
127
  requirements: []
120
128
  rubyforge_project:
121
- rubygems_version: 2.0.7
129
+ rubygems_version: 1.8.23
122
130
  signing_key:
123
- specification_version: 4
131
+ specification_version: 3
124
132
  summary: Property sets for ActiveRecord.
125
133
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: a0839379d91e7803975103fb3b43f06163a45d7d
4
- data.tar.gz: 71dea6d3b8cd20221c38712ecf4d5ecc254a1eae
5
- SHA512:
6
- metadata.gz: 642289a1daf0b276d680bbf5aea576ee536f06385f911fc1e10f3e3e8d09290e60a52520457df72cc48c85f458de5eadf50dfd2da45d7a0987f5eda76ba24a8f
7
- data.tar.gz: c5f60fd099c481062e2a3a74d73319c2fc7fcc1e80a64d56eb0f89329ba1d7928c055b0ffde961c31e8025720448e21a1ae025a7e1bb08be8b201d92ba719df8