entity_store 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/entity_store.rb CHANGED
@@ -2,6 +2,7 @@ module EntityStore
2
2
 
3
3
  require_relative 'entity_store/logging'
4
4
  require_relative 'entity_store/config'
5
+ require_relative 'entity_store/time_factory'
5
6
  require_relative 'entity_store/entity'
6
7
  require_relative 'entity_store/entity_value'
7
8
  require_relative 'entity_store/event'
@@ -1,3 +1,5 @@
1
+ require 'time' if respond_to?(:require)
2
+
1
3
  module EntityStore
2
4
  module Event
3
5
  attr_accessor :entity_id, :entity_version
@@ -25,11 +27,7 @@ module EntityStore
25
27
  names.each do |name|
26
28
  define_method "#{name}=" do |value|
27
29
  if value.kind_of?(String)
28
- # implementing parsing here rather than using std-lib Time.parse to
29
- # allow portability across platforms
30
- parts = /(?:(\d+))-(?:(\d+))-(?:(\d+))\s(?:(\d+)):(?:(\d+)):(?:(\d+))\s(?:(.+))/.match(value)
31
- offset = parts[7].gsub(/\d{4}/) do |m| m.scan(/../).join(":") end
32
- new_value = Time.new(parts[1].to_i, parts[2].to_i, parts[3].to_i, parts[4].to_i, parts[5].to_i, parts[6].to_i, offset)
30
+ new_value = TimeFactory.parse(value)
33
31
  else
34
32
  new_value = value
35
33
  end
@@ -14,16 +14,16 @@ module EntityStore
14
14
  end
15
15
 
16
16
  def database
17
- URI.parse(ExternalStore.connection_profile).path.gsub(/^\//, '')
17
+ @_database ||= URI.parse(ExternalStore.connection_profile).path.gsub(/^\//, '')
18
18
  end
19
19
  end
20
20
 
21
- def open_connection
21
+ def open
22
22
  ExternalStore.connection.db(ExternalStore.database)
23
23
  end
24
24
 
25
25
  def collection
26
- @_collection ||= open_connection['events']
26
+ @_collection ||= open['events']
27
27
  end
28
28
 
29
29
  def ensure_indexes
@@ -3,7 +3,7 @@ module EntityStore
3
3
 
4
4
  [:debug, :info, :warn].each do |level|
5
5
  define_method("log_#{level}") do |message=nil, &block|
6
- Config.logger.send(level, message || block) if Config.logger
6
+ Config.logger.send(level, message, &block) if Config.logger
7
7
  end
8
8
  end
9
9
 
@@ -15,7 +15,7 @@ module EntityStore
15
15
  end
16
16
 
17
17
  def database
18
- URI.parse(MongoEntityStore.connection_profile).path.gsub(/^\//, '')
18
+ @_database ||= URI.parse(MongoEntityStore.connection_profile).path.gsub(/^\//, '')
19
19
  end
20
20
  end
21
21
 
@@ -84,7 +84,7 @@ module EntityStore
84
84
  if attrs = entities.find_one('_id' => BSON::ObjectId.from_string(id))
85
85
  begin
86
86
  entity_type = EntityStore::Config.load_type(attrs['_type'])
87
- entity = entity_type.new(attrs['snapshot'] || {'id' => id, 'version' => attrs['version']})
87
+ entity = entity_type.new(attrs['snapshot'] || {'id' => id })
88
88
  rescue => e
89
89
  log_error "Error loading type #{attrs['_type']}", e
90
90
  raise
@@ -63,6 +63,7 @@ module EntityStore
63
63
  end
64
64
 
65
65
  def get(id, raise_exception=false)
66
+ log_debug { "Store#get #{id}"}
66
67
  if entity = storage_client.get_entity(id, raise_exception)
67
68
 
68
69
  storage_client.get_events(id, entity.version).each do |event|
@@ -0,0 +1,8 @@
1
+ module EntityStore
2
+ module TimeFactory
3
+ def self.parse(value)
4
+ require 'time'
5
+ Time.parse(value)
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module EntityStore
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.2.2".freeze
3
3
  end
@@ -35,4 +35,5 @@ describe EntityStore::Config do
35
35
  end
36
36
  end
37
37
  end
38
+
38
39
  end
@@ -39,7 +39,7 @@ describe MongoEntityStore do
39
39
  subject
40
40
  end
41
41
  it "should construct a new entity" do
42
- MongoEntityStoreSpec::DummyEntity.should_receive(:new).with({'id' => @id, 'version' => @version})
42
+ MongoEntityStoreSpec::DummyEntity.should_receive(:new).with({'id' => @id})
43
43
  subject
44
44
  end
45
45
  it "should return the entity" do
@@ -1 +1,58 @@
1
- require 'spec_helper'
1
+ require 'spec_helper'
2
+
3
+ class DummyEntity
4
+ include Entity
5
+
6
+ attr_accessor :name
7
+
8
+ def set_name(name)
9
+ record_event DummyEntityNameSet.new(name: name)
10
+ end
11
+ end
12
+
13
+ class DummyEntityNameSet
14
+ include Event
15
+
16
+ attr_accessor :name
17
+
18
+ def apply(entity)
19
+ entity.name = name
20
+ end
21
+ end
22
+
23
+ class DummyEntitySubscriber
24
+ class << self
25
+ attr_accessor :event_name
26
+ end
27
+
28
+ def dummy_entity_name_set(event)
29
+ DummyEntitySubscriber.event_name = event.name
30
+ end
31
+ end
32
+
33
+ describe "end to end" do
34
+ before(:each) do
35
+ MongoEntityStore.connection_profile = "mongodb://localhost/entity_store_test"
36
+
37
+ EntityStore::Config.setup do |config|
38
+ config.store = MongoEntityStore.new
39
+ config.event_subscribers << DummyEntitySubscriber
40
+ end
41
+ end
42
+
43
+ context "when save entity" do
44
+ let(:name) { random_string }
45
+ before(:each) do
46
+ @entity = DummyEntity.new
47
+ @entity.set_name name
48
+ @id = Store.new.save @entity
49
+ end
50
+
51
+ it "publishes event to the subscriber" do
52
+ DummyEntitySubscriber.event_name.should eq(name)
53
+ end
54
+ it "is retrievable with the events applied" do
55
+ EntityStore::Store.new.get(@entity.id).name.should eq(name)
56
+ end
57
+ end
58
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'rake'
2
2
  require 'rspec'
3
3
  require 'mongo'
4
+ require 'hatchet'
5
+
4
6
  require "#{Rake.application.original_dir}/lib/entity_store"
5
7
 
6
8
  RSpec.configure do |config|
@@ -9,10 +11,16 @@ end
9
11
 
10
12
  include EntityStore
11
13
 
12
- require 'logger'
13
- logger = ::Logger.new(STDOUT)
14
- logger.level = ::Logger::ERROR
15
- EntityStore::Config.logger = logger
14
+ Hatchet.configure do |config|
15
+ config.level :fatal
16
+ config.formatter = Hatchet::SimpleFormatter.new
17
+ config.appenders << Hatchet::LoggerAppender.new do |appender|
18
+ appender.logger = Logger.new(STDOUT)
19
+ end
20
+ end
21
+ include Hatchet
22
+
23
+ EntityStore::Config.logger = log
16
24
 
17
25
  def random_string
18
26
  (0...24).map{ ('a'..'z').to_a[rand(26)] }.join
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entity_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Adam Bird
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-03-30 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: hatchet
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -43,6 +46,7 @@ files:
43
46
  - lib/entity_store/mongo_entity_store.rb
44
47
  - lib/entity_store/not_found.rb
45
48
  - lib/entity_store/store.rb
49
+ - lib/entity_store/time_factory.rb
46
50
  - lib/entity_store/version.rb
47
51
  - lib/entity_store.rb
48
52
  - lib/tasks/entity_store.rake
@@ -58,26 +62,27 @@ files:
58
62
  - spec/spec_helper.rb
59
63
  homepage: http://github.com/adambird/entity_store
60
64
  licenses: []
61
- metadata: {}
62
65
  post_install_message:
63
66
  rdoc_options: []
64
67
  require_paths:
65
68
  - lib
66
69
  required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
67
71
  requirements:
68
72
  - - ! '>='
69
73
  - !ruby/object:Gem::Version
70
74
  version: '0'
71
75
  required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
72
77
  requirements:
73
78
  - - ! '>='
74
79
  - !ruby/object:Gem::Version
75
80
  version: '0'
76
81
  requirements: []
77
82
  rubyforge_project:
78
- rubygems_version: 2.0.3
83
+ rubygems_version: 1.8.24
79
84
  signing_key:
80
- specification_version: 4
85
+ specification_version: 3
81
86
  summary: Event sourced entity store with a replaceable body
82
87
  test_files:
83
88
  - spec/entity_store/config_spec.rb
@@ -90,3 +95,4 @@ test_files:
90
95
  - spec/entity_store/store_spec.rb
91
96
  - spec/entity_store_spec.rb
92
97
  - spec/spec_helper.rb
98
+ has_rdoc:
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MTNjMDA0M2EyMWNmMWM5Y2U1NDc2MjMwOWU0NWQ5Mjk0ZWFlYjUxOA==
5
- data.tar.gz: !binary |-
6
- ZWMzNDEzNTYzYzU4ZjE1ZjVjOGNkZTJhMjQxMWMzOWUwNTllOGUwNQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MThjMmU5N2RhMzVjMjRkMGJmNDFjOTMxNjU5OWI5MzU3NWJkYzI5NDM4OTJm
10
- ZjJjNzg3M2I4ZWJhMmIzMzUwYWFjOGU1Njk4OWMxY2Q0ZDA5ZGUyZjIzNGUz
11
- MmQyMDE5YzgyOTFhZDI4Y2RlYzQzZDQ4M2Y3Yjg3YjYyNTdjNTU=
12
- data.tar.gz: !binary |-
13
- NDE3ODdjNGI2MzJiYzlhZDk0OTA2NzkwOTBlM2I1OGNmYWRhMjBlMTAyZjIw
14
- ZTk0NmFmYTZmZWJjZTI3MmQzNDJkNmFlY2JhN2VkZWIzNGYzY2I2NTUxMDU1
15
- ZTdlY2NkMjk4ZmU1MjExNmYwZjM5Yjk2N2Y4YzE1ZmQ3M2M0ZWQ=