hyperion-api 0.1.0 → 0.1.1

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.
@@ -16,13 +16,19 @@ module Hyperion
16
16
  end
17
17
 
18
18
  def self.pack(type, &block)
19
- @packers ||= {}
20
- @packers[type] = block
19
+ packers[type] = block
20
+ end
21
+
22
+ def self.packer_defined?(type)
23
+ packers.has_key?(type)
21
24
  end
22
25
 
23
26
  def self.unpack(type, &block)
24
- @unpackers ||= {}
25
- @unpackers[type] = block
27
+ unpackers[type] = block
28
+ end
29
+
30
+ def self.unpacker_defined?(type)
31
+ unpackers.has_key?(type)
26
32
  end
27
33
 
28
34
  # Sets the active datastore
@@ -121,6 +127,14 @@ module Hyperion
121
127
 
122
128
  private
123
129
 
130
+ def self.packers
131
+ @packers ||= {}
132
+ end
133
+
134
+ def self.unpackers
135
+ @unpackers ||= {}
136
+ end
137
+
124
138
  def self.build_query(kind, args)
125
139
  kind = Format.format_kind(kind)
126
140
  filters = build_filters(args[:filters])
@@ -1,3 +1,5 @@
1
+ require 'hyperion/types'
2
+
1
3
  shared_examples_for 'Datastore' do
2
4
 
3
5
  def api
@@ -262,4 +264,25 @@ shared_examples_for 'Datastore' do
262
264
 
263
265
  end
264
266
  end
267
+
268
+ Hyperion.defentity(:shirt) do |kind|
269
+ kind.field(:account_key, :type => Hyperion::Types.foreign_key(:account))
270
+ end
271
+
272
+ Hyperion.defentity(:account) do |kind|
273
+ kind.field(:first_name)
274
+ end
275
+
276
+ context 'foreign_keys' do
277
+ it 'saves records with foreign keys' do
278
+ account = api.save(:kind => :account)
279
+ account_key = account[:key]
280
+ shirt = api.save(:kind => :shirt, :account_key => account_key)
281
+ found_shirt = api.find_by_key(shirt[:key])
282
+ found_account = api.find_by_key(account_key)
283
+ shirt[:account_key].should == account_key
284
+ found_shirt[:account_key].should == account_key
285
+ found_account[:key].should == account_key
286
+ end
287
+ end
265
288
  end
@@ -1,10 +1,10 @@
1
1
  require 'hyperion'
2
+ require 'hyperion/key'
2
3
 
3
4
  module Hyperion
4
5
  class Memory
5
6
 
6
7
  def initialize(opts={})
7
- @id_counter = 0
8
8
  @store = {}
9
9
  end
10
10
 
@@ -48,6 +48,14 @@ module Hyperion
48
48
  find(query).length
49
49
  end
50
50
 
51
+ def pack_key(kind, key)
52
+ key
53
+ end
54
+
55
+ def unpack_key(kind, key)
56
+ key
57
+ end
58
+
51
59
  private
52
60
 
53
61
  attr_reader :store
@@ -95,7 +103,7 @@ module Hyperion
95
103
  end
96
104
 
97
105
  def generate_key
98
- @id_counter += 1
106
+ Hyperion::Key.generate_id
99
107
  end
100
108
 
101
109
  def apply_offset(offset, records)
@@ -0,0 +1,22 @@
1
+ require 'hyperion'
2
+ require 'hyperion/format'
3
+
4
+ module Hyperion
5
+ class Types
6
+ class << self
7
+
8
+ def foreign_key(kind)
9
+ kind_key = "#{Format.format_kind(kind)}_key".to_sym
10
+ unless Hyperion.packer_defined?(kind_key)
11
+ Hyperion.pack(kind_key) do |key|
12
+ Hyperion.datastore.pack_key(kind, key)
13
+ end
14
+ Hyperion.unpack(kind_key) do |key|
15
+ Hyperion.datastore.unpack_key(kind, key)
16
+ end
17
+ end
18
+ kind_key
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,13 +1,15 @@
1
1
  module Hyperion
2
2
  class FakeDs
3
3
 
4
- attr_accessor :saved_records, :queries, :key_queries, :returns
4
+ attr_accessor :saved_records, :queries, :key_queries, :returns, :key_pack_queries,:key_unpack_queries
5
5
 
6
6
  def initialize(opts={})
7
7
  @saved_records = []
8
8
  @returns = []
9
9
  @queries = []
10
10
  @key_queries = []
11
+ @key_pack_queries = []
12
+ @key_unpack_queries = []
11
13
  end
12
14
 
13
15
  def save(records)
@@ -40,5 +42,13 @@ module Hyperion
40
42
  returns.shift || 0
41
43
  end
42
44
 
45
+ def pack_key(kind, key)
46
+ @key_pack_queries << {:kind => kind, :key => key}
47
+ end
48
+
49
+ def unpack_key(kind, key)
50
+ @key_unpack_queries << {:kind => kind, :key => key}
51
+ end
52
+
43
53
  end
44
54
  end
@@ -134,7 +134,16 @@ shared_examples_for 'record packing' do |actor|
134
134
  it 'prefers the custom packer over the type packer' do
135
135
  result = actor.call(:kind => :packable, :two_packer => 'thing')
136
136
  result[:two_packer].should == 'gniht'
137
+
138
+ Hyperion.defentity(:keyed) do |kind|
139
+ kind.field(:widget, :type => Integer)
140
+ kind.field(:downed, :type => :down)
141
+ kind.field(:thing, :type => :nested_type)
142
+ kind.field(:bauble, :packer => lambda {|value| value ? value.reverse : value})
143
+ kind.field(:bad_packer, :packer => true)
144
+ kind.field(:two_packer, :type => Integer, :packer => lambda {|value| value ? value.reverse : value})
137
145
  end
146
+ end
138
147
 
139
148
  context 'Timestamps' do
140
149
 
@@ -1,4 +1,5 @@
1
1
  require 'hyperion'
2
+ require 'hyperion/types'
2
3
  require 'hyperion/shared_examples'
3
4
  require 'hyperion/fake_ds'
4
5
 
@@ -56,6 +57,28 @@ describe Hyperion do
56
57
  end
57
58
  end
58
59
 
60
+ context 'packer_defined?' do
61
+ it 'false if a packer has not been defined for the given type' do
62
+ Hyperion.packer_defined?(:undefined_packer).should == false
63
+ end
64
+
65
+ it 'true if a packer has been defined for the given type' do
66
+ Hyperion.pack(:thing) {|value| value}
67
+ Hyperion.packer_defined?(:thing).should == true
68
+ end
69
+ end
70
+
71
+ context 'unpacker_defined?' do
72
+ it 'false if a packer has not been defined for the given type' do
73
+ Hyperion.unpacker_defined?(:undefined_packer).should == false
74
+ end
75
+
76
+ it 'true if a packer has been defined for the given type' do
77
+ Hyperion.unpack(:thing) {|value| value}
78
+ Hyperion.unpacker_defined?(:thing).should == true
79
+ end
80
+ end
81
+
59
82
  context 'with fake datastore' do
60
83
 
61
84
  def fake_ds
@@ -235,5 +258,40 @@ describe Hyperion do
235
258
  end
236
259
 
237
260
  end
261
+
262
+ Hyperion.defentity(:keyed) do |kind|
263
+ kind.field(:spouse_key, :type => Hyperion::Types.foreign_key(:spouse))
264
+ end
265
+
266
+ it 'returns the packer key' do
267
+ Hyperion::Types.foreign_key(:spouse).should == :spouse_key
268
+ end
269
+
270
+ it 'defines a packer for the given kind' do
271
+ Hyperion.packer_defined?(:spouse_key)
272
+ end
273
+
274
+ it 'defines an unpacker for the given kind' do
275
+ Hyperion.unpacker_defined?(:spouse_key)
276
+ end
277
+
278
+ it 'formats the kind' do
279
+ Hyperion::Types.foreign_key(:SPouse).should == :spouse_key
280
+ end
281
+
282
+ it 'asks the current datastore to pack the key' do
283
+ key = 'the key to pack'
284
+ Hyperion.save(:kind => :keyed, :spouse_key => key)
285
+ fake_ds.key_pack_queries.first[:kind].should == :spouse
286
+ fake_ds.key_pack_queries.first[:key].should == key
287
+ end
288
+
289
+ it 'asks the current datastore to unpack the key' do
290
+ key = 'the key to pack'
291
+ fake_ds.returns = [[{:kind => :keyed, :spouse_key => key}]]
292
+ Hyperion.save(:kind => :keyed)
293
+ fake_ds.key_unpack_queries.first[:kind].should == :spouse
294
+ fake_ds.key_unpack_queries.first[:key].should == key
295
+ end
238
296
  end
239
297
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-17 00:00:00.000000000 Z
12
+ date: 2012-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -58,6 +58,7 @@ files:
58
58
  - lib/hyperion/memory.rb
59
59
  - lib/hyperion/sort.rb
60
60
  - lib/hyperion/query.rb
61
+ - lib/hyperion/types.rb
61
62
  - lib/hyperion.rb
62
63
  - spec/hyperion_spec.rb
63
64
  - spec/hyperion/util_spec.rb