norton 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3dc27425e5727f91cb894d4b3604b5eaa35b9031
4
- data.tar.gz: f504cab091855d1ebdef3d5462563c36bfe38c54
3
+ metadata.gz: 45ea8d2f8971be12351a0cd129ed8a851f2a7823
4
+ data.tar.gz: bd77674e841f9dd8c8e2603a46b75a4cf03ed281
5
5
  SHA512:
6
- metadata.gz: c417f7fb3f1f0329ab01bd9e794abe5be6924ef09987de3ee7e8b3496e7937fd215f234d2d0bcee27bb562eaebb1549cb904145f39ff881fdccad1edcdbe266b
7
- data.tar.gz: 4797d39f200cde4d2bf5947fdc96b5cc6ed7723acc0ea10a817668a7912738b0dc852df5091c3898c54d9a636d2d891533943bff1adc92201944feb3eb090da4
6
+ metadata.gz: 1c4ca36139885b6ea121edc8c3b9ceb81ace3f9abe2c85e01f17c208f280748ef91cfc27b7f7d473e1fd4ca03cd79566193cf99e5e0b20de2273494212dce75f
7
+ data.tar.gz: 8e9c450ff7fc10a7d9c8007645ab60640f0be59549c2d952bd286e9babdfe023d3ad076fcf3ac46c27e9e993f6ab061b6cecdd02d693792dd0d9277aeb483fc0
data/CHANGLOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ### 0.1.1
2
2
 
3
+ - Fix `allow_nil` option for `timestamp` type. [#issue18](https://github.com/jianshucom/norton/issues/18)
4
+
5
+ ### 0.1.1
6
+
3
7
  - Fix `attribute` **get** method, using instance variable directly.
4
8
 
5
9
  ### 0.1.0
@@ -15,7 +15,7 @@ module Norton
15
15
  #
16
16
  # @return [type] [description]
17
17
  def counter(name, options={}, &blk)
18
- register_norton_value(name, :counter)
18
+ register_norton_value(name, :counter, options)
19
19
 
20
20
  # Redis: GET
21
21
  define_method(name) do
data/lib/norton/helper.rb CHANGED
@@ -15,12 +15,12 @@ module Norton
15
15
  #
16
16
  # @return [void]
17
17
  #
18
- def register_norton_value(name, norton_type)
18
+ def register_norton_value(name, norton_type, options = {})
19
19
  if !Norton::SUPPORTED_TYPES.include?(norton_type.to_sym)
20
20
  raise Norton::InvalidType.new("Norton Type: #{norton_type} invalid!")
21
21
  end
22
22
 
23
- @norton_values[name.to_sym] = norton_type.to_sym
23
+ @norton_values[name.to_sym] = options.symbolize_keys.merge(:type => norton_type.to_sym)
24
24
  end
25
25
 
26
26
  #
@@ -42,7 +42,7 @@ module Norton
42
42
  # @return [Symbol]
43
43
  #
44
44
  def norton_value_type(name)
45
- norton_values[name.to_sym]
45
+ norton_values.dig(name.to_sym, :type)
46
46
  end
47
47
  end
48
48
 
@@ -109,14 +109,20 @@ module Norton
109
109
  def assign_values(new_values)
110
110
  new_values.each do |field, val|
111
111
  type = self.class.norton_value_type(field)
112
- next unless %i[counter timestamp].include?(type)
113
112
 
114
- value = cast_value(type, val || try("#{field}_default_value"))
115
- instance_variable_set("@#{field}", value)
116
-
117
- if type == :timestamp && val.nil? && !try("#{field}_default_value").nil?
118
- Norton.redis.with do |conn|
119
- conn.set(norton_value_key(field), value)
113
+ case type
114
+ when :counter
115
+ value = cast_value(type, val || try("#{field}_default_value"))
116
+ instance_variable_set("@#{field}", value)
117
+ when :timestamp
118
+ if !val.nil?
119
+ instance_variable_set("@#{field}", cast_value(type, val))
120
+ elsif self.class.norton_values[field][:allow_nil]
121
+ instance_variable_set("@#{field}", nil)
122
+ else
123
+ value = cast_value(type, try("#{field}_default_value"))
124
+ instance_variable_set("@#{field}", value)
125
+ Norton.redis.with { |conn| conn.set(norton_value_key(field), value) }
120
126
  end
121
127
  end
122
128
  end
@@ -14,19 +14,23 @@ module Norton
14
14
  #
15
15
  # @return [type] [description]
16
16
  def timestamp(name, options={})
17
- register_norton_value(name, :timestamp)
17
+ register_norton_value(name, :timestamp, options)
18
18
 
19
19
  # Redis: GET
20
20
  define_method(name) do
21
- instance_variable_get("@#{name}") || begin
22
- value = Norton.redis.with do |conn|
23
- raw_value = conn.get(norton_value_key(name))
24
- break raw_value if raw_value.present?
21
+ return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}")
25
22
 
26
- send("#{name}_default_value").tap do |default_value|
27
- conn.set(norton_value_key(name), default_value)
28
- end
23
+ value = Norton.redis.with do |conn|
24
+ raw_value = conn.get(norton_value_key(name))
25
+ break raw_value if raw_value.present?
26
+
27
+ send("#{name}_default_value").tap do |default_value|
28
+ conn.set(norton_value_key(name), default_value)
29
29
  end
30
+ end
31
+ if value.nil? && options[:allow_nil]
32
+ instance_variable_set("@#{name}", nil)
33
+ else
30
34
  instance_variable_set("@#{name}", value.to_i)
31
35
  end
32
36
  end
@@ -1,3 +1,3 @@
1
1
  module Norton
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -35,7 +35,13 @@ describe Norton::Helper do
35
35
 
36
36
  it "adds the fields with valid type to `@norton_values`" do
37
37
  Dummy.register_norton_value("foo", "counter")
38
- expect(Dummy.norton_values[:foo]).to eq(:counter)
38
+ expect(Dummy.norton_values[:foo][:type]).to eq(:counter)
39
+ end
40
+
41
+ it "adds the fields with options" do
42
+ Dummy.register_norton_value("foo", "counter", :allow_nil => true)
43
+ expect(Dummy.norton_values[:foo][:type]).to eq(:counter)
44
+ expect(Dummy.norton_values[:foo][:allow_nil]).to eq(true)
39
45
  end
40
46
  end
41
47
 
@@ -158,6 +164,7 @@ describe Norton::Helper do
158
164
 
159
165
  dummy.norton_mget(:time2)
160
166
 
167
+ expect(dummy.time2).to be_nil
161
168
  expect(
162
169
  Norton.redis.with { |conn| conn.exists(dummy.norton_value_key(:time2)) }
163
170
  ).to eq(false)
@@ -20,6 +20,11 @@ describe Norton::Timestamp do
20
20
  expect(dummy.respond_to?(:born_at_default_value)).to be(true)
21
21
  expect(dummy.respond_to?(:touch_born_at)).to be(true)
22
22
  expect(dummy.respond_to?(:remove_born_at)).to be(true)
23
+
24
+ expect(Dummy.norton_values[:thirteen_ts][:type]).to eq(:timestamp)
25
+ expect(Dummy.norton_values[:thirteen_ts][:digits]).to eq(13)
26
+ expect(Dummy.norton_values[:first_kissed_at][:allow_nil]).to eq(true)
27
+ expect(Dummy.norton_values[:first_kissed_at][:allow_nil]).to eq(true)
23
28
  end
24
29
 
25
30
  describe "#born_at" do
@@ -41,6 +46,10 @@ describe Norton::Timestamp do
41
46
  end
42
47
  expect(value.to_i).to eq(456)
43
48
  end
49
+
50
+ it "returns nil if no value in norton and the timestamp allows nil" do
51
+ expect(dummy.first_kissed_at).to be_nil
52
+ end
44
53
  end
45
54
 
46
55
  describe "#born_at_default_value" do
data/spec/norton_spec.rb CHANGED
@@ -16,6 +16,7 @@ describe Norton do
16
16
 
17
17
  counter :counter1
18
18
  timestamp :time1
19
+ timestamp :time2, :allow_nil => true
19
20
  hash_map :map1
20
21
 
21
22
  def id
@@ -94,6 +95,7 @@ describe Norton do
94
95
 
95
96
  dummy.norton_mget(:time2)
96
97
 
98
+ expect(dummy.time2).to be_nil
97
99
  expect(
98
100
  Norton.redis.with { |conn| conn.exists(dummy.norton_value_key(:time2)) }
99
101
  ).to eq(false)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: norton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Larry Zhao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-30 00:00:00.000000000 Z
11
+ date: 2017-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis