acts_as_hashish 0.4.2 → 0.4.3

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.
@@ -1,9 +1,12 @@
1
1
  module Hashish
2
2
 
3
3
  def acts_as_hashish(options = {})
4
- raise "Cannot act as hashish without a redis connection!" unless Hashish.redis_connection
4
+ unless options.keys.all?{|k| k.is_a?(Symbol)} and options.values.all?{|v| v.is_a?(String) or v.is_a?(Hash)}
5
+ raise "Keys can only be Symbol, values can only be String or Hash"
6
+ end
7
+ raise "Cannot act as hashish without a redis connection" unless Hashish.redis_connection
5
8
  options[:key_prefix] ||= Hashish.redis_namespace + ':' + self.to_s
6
- raise "Please specify a primary index via the :key option!" unless options[:key]
9
+ raise "Please specify a primary index via the :key option" unless options[:key]
7
10
  options[:indexes] ||= {}
8
11
  options[:sorters] ||= {}
9
12
 
@@ -50,8 +50,8 @@ module Hashish
50
50
  data = o.to_json
51
51
  prefix = @options[:key_prefix]
52
52
  key = hashish_get_key(o)
53
- hashish_delete(key)
54
53
  raise "Error: Computed data key as '#{key}'. Only alphanumeric characters and underscore allowed!" unless key.to_s =~ /^[\w_]+$/
54
+ hashish_delete(key)
55
55
  Hashish.redis_connection.multi do
56
56
  Hashish.redis_connection.zadd("#{prefix}*", t , "#{prefix}@#{key}")
57
57
  # Hashish.redis_connection.zremrangebyrank("#{prefix}:", 0, -(@options[:max_size] + 1)) if @options[:max_size]
@@ -1,3 +1,3 @@
1
1
  module Hashish
2
- VERSION = '0.4.2'
2
+ VERSION = '0.4.3'
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'set'
3
2
 
4
3
  describe Hashish do
5
4
 
@@ -8,6 +7,7 @@ describe Hashish do
8
7
  @sample_data_2 = {'id' => 3, 'name' => 'Doe', 'location' => 'London'}
9
8
  @sample_data_3 = {'id' => 2, 'name' => 'Moe', 'location' => 'Mumbai'}
10
9
  @sample_data_4 = {'id' => 4, 'name' => 'Loe', 'location' => 'Goa'}
10
+ @sample_data_5 = {'id' => 5, 'name' => ['Zoe', 'Foe'], 'location' => 'Stockholm'}
11
11
 
12
12
  @primary_key = 'id'
13
13
 
@@ -41,16 +41,31 @@ describe Hashish do
41
41
  Hashish.redis_connection.zscore("#{@options[:key_prefix]}*", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_1)}").should_not be_nil
42
42
  end
43
43
 
44
+ it "should validate against hash or array only" do
45
+ expect { SampleClass.hashish_insert("")}.to raise_exception
46
+ end
47
+
44
48
  it "should overwrite data and indexes if an existing key is being re-inserted with new values" do
45
49
  SampleClass.hashish_insert(@sample_data_1)
46
50
  SampleClass.hashish_insert(@sample_data_1.merge('name' => 'Doe'))
47
51
  SampleClass.hashish_list(:filters => {'_name' => 'Joe'}).should == []
48
52
  end
49
53
 
50
- it "should create appropriate indexes" do
51
- SampleClass.hashish_insert(@sample_data_1)
52
- Hashish.redis_connection.zscore("#{@options[:key_prefix]}!#{@index_name}=#{@sample_data_1[@index_value]}", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_1)}").should_not be_nil
53
- Hashish.redis_connection.zscore("#{@options[:key_prefix]}!#{@proc_index_name}=#{@proc_index_value.call(@sample_data_1)}", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_1)}").should_not be_nil
54
+ context "when indexes with multiple values are passed" do
55
+ it "should create appropriate indexes" do
56
+ SampleClass.hashish_insert(@sample_data_5)
57
+ @sample_data_5[@index_value].each do |index_value|
58
+ Hashish.redis_connection.zscore("#{@options[:key_prefix]}!#{@index_name}=#{index_value}", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_5)}").should_not be_nil
59
+ end
60
+ end
61
+ end
62
+
63
+ context "when indexes with single values are passed" do
64
+ it "should create appropriate indexes" do
65
+ SampleClass.hashish_insert(@sample_data_1)
66
+ Hashish.redis_connection.zscore("#{@options[:key_prefix]}!#{@index_name}=#{@sample_data_1[@index_value]}", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_1)}").should_not be_nil
67
+ Hashish.redis_connection.zscore("#{@options[:key_prefix]}!#{@proc_index_name}=#{@proc_index_value.call(@sample_data_1)}", "#{@options[:key_prefix]}@#{SampleClass.send(:hashish_get_key, @sample_data_1)}").should_not be_nil
68
+ end
54
69
  end
55
70
 
56
71
  it "should create appropriate sorters" do
@@ -75,6 +90,14 @@ describe Hashish do
75
90
  end
76
91
 
77
92
  describe ".hashish_list" do
93
+ it "should return only elements inserted between specified time" do
94
+ t = Time.now
95
+ SampleClass.hashish_insert(@sample_data_1)
96
+ Time.stubs(:now).returns(t + 3600)
97
+ SampleClass.hashish_insert(@sample_data_2)
98
+ SampleClass.hashish_list(:from => (t + 1800).to_i).should == [@sample_data_2]
99
+ end
100
+
78
101
  it "should return all the items from the list" do
79
102
  test_data = [@sample_data_2, @sample_data_1, @sample_data_3]
80
103
  test_data.each do |data|
@@ -6,6 +6,14 @@ require 'rspec/autorun'
6
6
  require 'test/unit'
7
7
  require 'mocha'
8
8
 
9
+ require 'simplecov'
10
+ SimpleCov.start do
11
+ add_filter '/spec/'
12
+ end
13
+
14
+ require 'coveralls'
15
+ Coveralls.wear!
16
+
9
17
  # Requires supporting ruby files with custom matchers and macros, etc,
10
18
  # in spec/support/ and its subdirectories.
11
19
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: acts_as_hashish
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.2
5
+ version: 0.4.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Schubert Cardozo
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-04-29 00:00:00 Z
13
+ date: 2013-04-30 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: redis
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - ~>
21
+ - - ">="
22
22
  - !ruby/object:Gem::Version
23
23
  version: 3.0.2
24
24
  type: :runtime
@@ -29,7 +29,7 @@ dependencies:
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
- - - ~>
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: 1.6.6
35
35
  type: :runtime
@@ -67,6 +67,28 @@ dependencies:
67
67
  version: 0.12.7
68
68
  type: :development
69
69
  version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: simplecov
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 0.7.1
79
+ type: :development
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: coveralls
83
+ prerelease: false
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.6.7
90
+ type: :development
91
+ version_requirements: *id007
70
92
  description: A sortable and searchable list backed by Redis. You can use it as a quick actionable workflow queue which you can search/sort and paginate thorough
71
93
  email:
72
94
  - cardozoschubert@gmail.com