propertybase_id 0.2.0 → 0.3.0

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: 8643adb6124aba399189a527653bdcd28280a778
4
- data.tar.gz: 76bd327742cfb04ee9e29befc306b91425b32347
3
+ metadata.gz: 1333ef678322fc89dfff3adc990624e6f17c25de
4
+ data.tar.gz: 44f789d27adb46ce42b3a90e667f6dd7dc193f8c
5
5
  SHA512:
6
- metadata.gz: 5298e1e086d89822a60397eb8c17b048e75d8ba8a9e9be46b44768105b01cefb0a8d07abb899056e3447be82b0f621472cedd1e6a6731f2cac07e878a4d4c26d
7
- data.tar.gz: 874bdfc5ca289a7ea4c253ad9bc88ec1eab948b5e65f556d42e9fa307dadaade091a6cb00cf790b5b29cc2d8bb4607e29a02996a8779d5727afe98c1dcb4c9f1
6
+ metadata.gz: 36b54a8284b3341635c8ff522c5eb667f7b8361ba4017168b8fc33dd371eb0d2803328bcf451b9f734b211abfedfda02175167f5b76755c209fde471bd813314
7
+ data.tar.gz: f5582927e52d8f718a06e82bec071678f89c102a87be5d11e5e63cbb299ea1c08d8255c1ff6f4d13fa40b586febfea81aeb0ae3810dbbfdba7f3639b0e733fbb
data/.travis.yml CHANGED
@@ -2,10 +2,8 @@ language: ruby
2
2
  cache: bundler
3
3
  bundler_args: --without documentation production
4
4
  rvm:
5
- - 2.0.0
6
5
  - 2.1.5
7
- - 2.2.0
8
- - jruby-19mode
6
+ - 2.2.1
9
7
  - rbx
10
8
  - ruby-head
11
9
  - jruby-head
data/README.md CHANGED
@@ -20,22 +20,22 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- ### Create
23
+ ### Generate
24
24
 
25
- id = PropertybaseId.create(objectt: "team", server: 1)
26
- => #<PropertybaseId:0x007fe855071a58 @_object_id=2, @local_random=738414242805187245, @object="team", @server=1>
25
+ id = PropertybaseId.generate(object: "team")
26
+ => #<PropertybaseId:0x007f90e3dcf048 @counter=1, @host_id=1203, @object_id=2, @process_id=254, @time=1427867006>
27
27
 
28
28
  For string representation do:
29
29
 
30
30
  id.to_s
31
- => "02015lyq044bfuel"
31
+ => "002xfnm458e7200001"
32
32
 
33
33
  ### Parsing
34
34
 
35
35
  To get a PropertybaseId object from a string representation do:
36
36
 
37
- PropertybaseId.parse("02015lyq044bfuel")
38
- => #<PropertybaseId:0x007f9943cb48a8 @_object_id=2, @local_random=738414242805187245, @object="team", @server=1>
37
+ PropertybaseId.parse("002xfnm458e7200001")
38
+ => #<PropertybaseId:0x007f90e3d57f48 @counter=1, @host_id=1203, @object_id=2, @process_id=254, @time=1427867006>
39
39
 
40
40
  ## Development
41
41
 
@@ -1,4 +1,5 @@
1
1
  require "digest/sha1"
2
+ require "socket"
2
3
 
3
4
  class PropertybaseId
4
5
  class Generator
@@ -8,26 +9,23 @@ class PropertybaseId
8
9
  end
9
10
 
10
11
  def generate(object: )
11
- @mutex.lock
12
- begin
13
- count = next_counter
14
- ensure
15
- @mutex.unlock rescue nil
12
+ @mutex.synchronize do
13
+ PropertybaseId.new(
14
+ object_id: pb_object_id(object),
15
+ host_id: host_id,
16
+ time: ::Time.now.to_i,
17
+ process_id: process_id,
18
+ counter: next_counter
19
+ )
16
20
  end
17
-
18
- PropertybaseId.new(
19
- object: object,
20
- host_id: host_id,
21
- time: time,
22
- process_id: process_id,
23
- counter: count
24
- )
25
21
  end
26
22
 
27
23
  private
28
24
 
29
- def time
30
- @_time ||= ::Time.now.to_i
25
+ def pb_object_id(object)
26
+ PropertybaseId::Mappings.objects.fetch(object) do
27
+ raise ArgumentError, "Object #{object.inspect} not found"
28
+ end
31
29
  end
32
30
 
33
31
  def host_id
@@ -35,7 +33,7 @@ class PropertybaseId
35
33
  end
36
34
 
37
35
  def next_counter
38
- @counter = (@counter + 1) % max_value(3)
36
+ @counter = (@counter + 1) % max_value(5)
39
37
  end
40
38
 
41
39
  def process_id
@@ -1,3 +1,3 @@
1
1
  class PropertybaseId
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,22 +1,24 @@
1
1
  require "propertybase_id/version"
2
- require "propertybase_id/local_random"
3
2
  require "propertybase_id/generator"
4
3
  require "propertybase_id/mappings"
5
4
 
6
5
  class PropertybaseId
7
- attr_reader :object
6
+ attr_reader :object_id
8
7
  attr_reader :host_id
9
8
  attr_reader :time
10
9
  attr_reader :process_id
11
10
  attr_reader :counter
12
11
 
13
- def initialize(object:, host_id:, time:, process_id:, counter:)
14
- @object = object && object.dup.freeze
12
+ def initialize(object_id:, host_id:, time:, process_id:, counter:)
13
+ @object_id = object_id
15
14
  @host_id = host_id
16
15
  @time = time
17
16
  @process_id = process_id
18
17
  @counter = counter
19
- validate!
18
+ end
19
+
20
+ def object
21
+ @_object ||= self.class.team_from_object_id(@object_id || "")
20
22
  end
21
23
 
22
24
  def to_s
@@ -25,7 +27,7 @@ class PropertybaseId
25
27
  host_str = format_number(@host_id, 2)
26
28
  time_str = format_number(@time, 6)
27
29
  process_str = format_number(@process_id, 2)
28
- counter_str = format_number(@counter, 3)
30
+ counter_str = format_number(@counter, 5)
29
31
 
30
32
  "#{object_str}#{host_str}#{time_str}#{process_str}#{counter_str}"
31
33
  end
@@ -33,7 +35,7 @@ class PropertybaseId
33
35
 
34
36
  def ==(o)
35
37
  self.class == o.class &&
36
- self.object == o.object &&
38
+ self.object_id == o.object_id &&
37
39
  self.host_id == o.host_id &&
38
40
  self.process_id == o.process_id &&
39
41
  self.time == o.time &&
@@ -44,32 +46,27 @@ class PropertybaseId
44
46
 
45
47
  def hash
46
48
  [
47
- @object_id,
48
- @host_id,
49
- @time,
50
- @process_id,
51
- @counter,
49
+ object_id,
50
+ host_id,
51
+ time,
52
+ process_id,
53
+ counter,
52
54
  ].hash
53
55
  end
54
56
 
55
57
  def self.generate(object:)
56
- @_generator ||= begin
57
- PropertybaseId::Generator.new
58
- end
59
- @_generator.generate(object: object)
58
+ @@generator.generate(object: object)
60
59
  end
61
60
 
62
61
  def self.parse(input_id)
63
- raise ArgumentError, "invalid length (#{input_id.size})" if input_id.size != 16
62
+ raise ArgumentError, "invalid length (#{input_id.size})" if input_id.size != 18
64
63
 
65
- _, object_id, host_id, time, process_id, counter = input_id.match(/(\w{3})(\w{2})(\w{6})(\w{2})(\w{3})/).to_a
64
+ _, object_id, host_id, time, process_id, counter = input_id.match(/(\w{3})(\w{2})(\w{6})(\w{2})(\w{5})/).to_a
66
65
 
67
- object, _ = PropertybaseId::Mappings.objects.select{|_, v| v == object_id.to_i(36) }.first
68
-
69
- raise ArgumentError, "No object to id #{object_id}" if object.nil?
66
+ team_from_object_id(object_id.to_i(36))
70
67
 
71
68
  new(
72
- object: object,
69
+ object_id: object_id.to_i(36),
73
70
  host_id: host_id.to_i(36),
74
71
  time: time.to_i(36),
75
72
  process_id: process_id.to_i(36),
@@ -79,13 +76,15 @@ class PropertybaseId
79
76
 
80
77
  private
81
78
 
82
- def validate!
83
- @object_id ||= PropertybaseId::Mappings.objects.fetch(object) do
84
- raise ArgumentError, "Object #{object.inspect} not found"
85
- end
86
- end
87
-
88
79
  def format_number(integer, length)
89
80
  integer.to_s(36).rjust(length, "0")
90
81
  end
82
+
83
+ def self.team_from_object_id(input_id)
84
+ obj, _ = PropertybaseId::Mappings.objects.select{|_, v| v == input_id }.first
85
+ raise ArgumentError, "No object to id #{input_id.to_s(36)}" if obj.nil?
86
+ obj
87
+ end
88
+
89
+ @@generator = PropertybaseId::Generator.new
91
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propertybase_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leif Gensert
@@ -73,7 +73,6 @@ files:
73
73
  - lib/propertybase_id.rb
74
74
  - lib/propertybase_id/data/objects.json
75
75
  - lib/propertybase_id/generator.rb
76
- - lib/propertybase_id/local_random.rb
77
76
  - lib/propertybase_id/mappings.rb
78
77
  - lib/propertybase_id/version.rb
79
78
  - propertybase_id.gemspec
@@ -1,23 +0,0 @@
1
- class PropertybaseId
2
- class LocalRandom
3
- MAX_INTEGER = 10000
4
- TIME_MULTIPLIER = 10000
5
-
6
- attr_reader :time
7
-
8
- def initialize(time=Time.now)
9
- @time = time
10
- end
11
-
12
- def to_i
13
- @_integer ||= begin
14
- rand_int = SecureRandom.random_number(MAX_INTEGER)
15
- max_digits = (MAX_INTEGER - 1).to_s.size
16
- prefix = "%0#{max_digits}d" % rand_int
17
- time_with_miliseconds = (time.to_f * TIME_MULTIPLIER).to_i
18
-
19
- "#{prefix}#{time_with_miliseconds}".to_i
20
- end
21
- end
22
- end
23
- end