trax_model 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17e59a1849ad78e2b278f59a71ceef88bbf34416
4
- data.tar.gz: 26557fe84e77a9fb2091f6f5433c4ea06da81edc
3
+ metadata.gz: aadee7f297ba78ffcf101d66d949fd921528e6aa
4
+ data.tar.gz: 31fc37ab41fbef66ac534734b9c34d223cdc71b7
5
5
  SHA512:
6
- metadata.gz: 5f9217c38ac8bfa6e3be1236c0ea3fb0ae5bb31da21214c68b6a100da4f383c856f222c6823185e01909f707d967cd740a38e09d297860c4333198487b8a4b01
7
- data.tar.gz: 4604d68478af8603c4168bd2aa73c71a57db9db718482806f010c6a7f92077889a4604aaac1aa9865c7f89d5e551216005f6922efb135db614cbf9c7c0b9588f
6
+ metadata.gz: 135a818db486a91cace4e5787d3de8d7c1685247df963f861af5fc6aabbe0054e0def7f60f66d6bbca2c2cea6f2b690320d5da98fecdf292bfff72a11e913e4c
7
+ data.tar.gz: d9df19307c0741ac126a5c9a574715f393e4a1947096a1cb17114466d649720ea5f208e8eba65a6dcfdeb815ba37d7474a2cb959df3343a09ec3406ff4bfc4dd
data/Gemfile CHANGED
@@ -1,7 +1,3 @@
1
- source 'https://8JwshTDZWLNCc53CjfAb@gem.fury.io/jasonayre/'
2
1
  source 'https://rubygems.org'
3
2
 
4
- gem 'trax_core', :git => 'git@github.com:jasonayre/trax_core.git'
5
-
6
- # Specify your gem's dependencies in trax_model.gemspec
7
3
  gemspec
@@ -3,7 +3,6 @@ require 'default_value_for'
3
3
  require 'hashie/dash'
4
4
  require 'hashie/mash'
5
5
  require_relative './string'
6
-
7
6
  require_relative './validators/email_validator'
8
7
  require_relative './validators/frozen_validator'
9
8
  require_relative './validators/future_validator'
@@ -19,6 +18,7 @@ module Trax
19
18
  autoload :Freezable
20
19
  autoload :Registry
21
20
  autoload :UUID
21
+ autoload :UUIDPrefix
22
22
  autoload :UniqueId
23
23
  autoload :Matchable
24
24
  autoload :Validators
@@ -39,7 +39,6 @@ module Trax
39
39
  delegate :register_trax_model, :to => "::Trax::Model::Registry"
40
40
  delegate :[], :to => :find
41
41
 
42
-
43
42
  def defaults(options = {})
44
43
  options.each_pair do |key, val|
45
44
  self.trax_defaults.__send__("#{key}=", val)
@@ -4,7 +4,8 @@ module Trax
4
4
  ERROR_MESSAGES = {
5
5
  :invalid_uuid_prefix => [
6
6
  "UUID prefix must be 2 characters long",
7
- "and can only include a-f0-9",
7
+ "First Character must be an integer 0-9",
8
+ "Second character must be a letter a-f",
8
9
  "for hexadecimal id compatibility"
9
10
  ].join("\n")
10
11
  }.freeze
@@ -13,11 +14,11 @@ module Trax
13
14
  property :uuid_column, :default => :id
14
15
 
15
16
  def uuid_prefix=(prefix)
16
- if prefix.length != 2 || prefix !~ /[a-f0-9]{2}/
17
+ if prefix.length != 2 || prefix.chars.first !~ /[0-9]/ || prefix.chars.last !~ /[a-f]/
17
18
  raise ERROR_MESSAGES[:invalid_uuid_prefix]
18
19
  end
19
20
 
20
- self[:uuid_prefix] = prefix
21
+ self[:uuid_prefix] = ::Trax::Model::UUIDPrefix.new(prefix)
21
22
  end
22
23
  end
23
24
  end
@@ -8,6 +8,11 @@ module Trax
8
8
  class << self
9
9
  delegate :key?, :to => :models
10
10
  delegate :each, :to => :models
11
+ delegate :all, :to => :collection
12
+ end
13
+
14
+ def self.collection
15
+ models.try(:values)
11
16
  end
12
17
 
13
18
  def self.register_trax_model(model)
@@ -22,6 +27,30 @@ module Trax
22
27
  uuid_map.fetch(prefix)
23
28
  end
24
29
 
30
+ def self.next_prefix
31
+ current_prefix = models.values
32
+ .reject!{|model| !model.try(:uuid_prefix) }
33
+ .map(&:uuid_prefix)
34
+ .sort
35
+ .last
36
+
37
+ current_prefix.next
38
+ end
39
+
40
+ def self.model_prefixes
41
+ models.try(:keys)
42
+ end
43
+
44
+ def self.previous_prefix
45
+ current_prefix = models.values
46
+ .reject!{|model| !model.try(:uuid_prefix) }
47
+ .map(&:uuid_prefix)
48
+ .sort
49
+ .last
50
+
51
+ current_prefix.previous
52
+ end
53
+
25
54
  def self.uuid_map
26
55
  @uuid_map ||= models.values.reject!{|model| !model.try(:uuid_prefix) }.inject(::Hashie::Mash.new) do |result, model|
27
56
  result[model.uuid_prefix] = model
@@ -0,0 +1,80 @@
1
+ module Trax
2
+ module Model
3
+ class UUIDPrefix < String
4
+ HEX_LETTER_RANGE = ('a'..'f').to_a.freeze
5
+ HEX_DIGIT_RANGE = (0..9).to_a.freeze
6
+ #9f = 96 so we need to begin at 96 when mapping back down [str,int]
7
+ DIVIDING_VALUE = 96
8
+
9
+ def self.all
10
+ @all ||= begin
11
+ lower_value_prefixes = HEX_DIGIT_RANGE.map do |digit|
12
+ values = HEX_LETTER_RANGE.map do |character|
13
+ prefix = [digit, character].join
14
+ new(prefix)
15
+ end
16
+
17
+ values
18
+ end
19
+
20
+ higher_value_prefixes = HEX_LETTER_RANGE.map do |digit|
21
+ values = HEX_DIGIT_RANGE.map do |character|
22
+ prefix = [digit, character].join
23
+ new(prefix)
24
+ end
25
+
26
+ values
27
+ end
28
+
29
+ [lower_value_prefixes, higher_value_prefixes].flatten!.sort!
30
+ end
31
+ end
32
+
33
+ def self.build_from_integer(value)
34
+ value_str = "#{value}"
35
+ return new([value_str.chars.first.to_i, HEX_LETTER_RANGE[value_str.chars.last.to_i]].join)
36
+ end
37
+
38
+ def <=>(comparison_prefix)
39
+ self.to_i <=> comparison_prefix.to_i
40
+ end
41
+
42
+ def in_higher_partition?
43
+ "#{self}"[0] =~ /[a-f]/
44
+ end
45
+
46
+ def in_lower_partition?
47
+ !in_higher_partition?
48
+ end
49
+
50
+ def index
51
+ self.class.all.index(self)
52
+ end
53
+
54
+ def next
55
+ self.class.all.at(index + 1)
56
+ end
57
+
58
+ def previous
59
+ self.class.all.at(index - 1)
60
+ end
61
+
62
+ def to_digits
63
+ digits = self.chars.map do |character|
64
+ (character.downcase =~ /[a-f]/ ? HEX_LETTER_RANGE.index(character.downcase) : character)
65
+ end
66
+
67
+ #kind of wonky but multiply by 2 will ensure the letter prefixes begin with a higher value
68
+ in_higher_partition? ? "#{((digits.join.to_i + DIVIDING_VALUE * 2))}".chars : digits
69
+ end
70
+
71
+ def to_i
72
+ return self.to_digits.join.to_i
73
+ end
74
+
75
+ def to_s
76
+ "#{self}"
77
+ end
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,3 @@
1
1
  module TraxModel
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -41,19 +41,26 @@ ActiveRecord::Schema.define(:version => 1) do
41
41
  t.datetime "created_at", :null => false
42
42
  t.datetime "updated_at", :null => false
43
43
  end
44
+
45
+ create_table "things", :force => true do |t|
46
+ t.string "name"
47
+ t.string "uuid"
48
+ t.datetime "created_at", :null => false
49
+ t.datetime "updated_at", :null => false
50
+ end
44
51
  end
45
52
 
46
53
  class Product < ::ActiveRecord::Base
47
54
  include ::Trax::Model
48
55
  include ::Trax::Model::UniqueId
49
56
 
50
- defaults :uuid_prefix => "a1", :uuid_column => "uuid"
57
+ defaults :uuid_prefix => "1a", :uuid_column => "uuid"
51
58
  end
52
59
 
53
60
  class Widget < ::ActiveRecord::Base
54
61
  include ::Trax::Model
55
62
 
56
- defaults :uuid_prefix => "a2", :uuid_column => "uuid"
63
+ defaults :uuid_prefix => "2a", :uuid_column => "uuid"
57
64
 
58
65
  validates :subdomain, :subdomain => true, :allow_nil => true
59
66
  validates :email_address, :email => true, :allow_nil => true
@@ -64,7 +71,7 @@ class Message < ::ActiveRecord::Base
64
71
  include ::Trax::Model
65
72
  include ::Trax::Model::Freezable
66
73
 
67
- defaults :uuid_prefix => "a3", :uuid_column => "uuid"
74
+ defaults :uuid_prefix => "3a", :uuid_column => "uuid"
68
75
 
69
76
  enum :status => [:queued, :scheduled, :delivered, :failed_delivery]
70
77
 
@@ -76,3 +83,9 @@ class Message < ::ActiveRecord::Base
76
83
 
77
84
  freezable_by_enum :status => [:delivered, :failed_delivery]
78
85
  end
86
+
87
+ class Thing < ::ActiveRecord::Base
88
+ include ::Trax::Model
89
+
90
+ defaults :uuid_prefix => "0a", :uuid_column => "uuid"
91
+ end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+
2
3
  describe ::Trax::Model::Config do
3
4
  describe "uuid_prefix" do
4
5
  context "bad prefixes" do
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+
2
3
  describe ::Trax::Model::Matchable do
3
4
  let(:product) { ::Product.create(:name => "27 inch iMac") }
4
5
  subject{ product }
@@ -4,7 +4,7 @@ describe ::Trax::Model::Registry do
4
4
  subject{ described_class }
5
5
 
6
6
  its(:models) { should be_instance_of(::Hashie::Mash) }
7
- its(:uuid_map) { should have_key("a1") }
7
+ its(:uuid_map) { should have_key("1a") }
8
8
  its(:uuid_map) { should be_instance_of(::Hashie::Mash) }
9
9
 
10
10
  it "should have registered product model" do
@@ -12,6 +12,6 @@ describe ::Trax::Model::Registry do
12
12
  end
13
13
 
14
14
  it "model_type_for_uuid" do
15
- subject.model_type_for_uuid("a1asdasdasd").should eq Product
15
+ subject.model_type_for_uuid("1absdasdasd").should eq Product
16
16
  end
17
17
  end
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
  describe ::Trax::Model::UniqueId do
3
3
  subject{ ::Product }
4
4
 
5
+ its(:uuid_prefix) {
6
+ should be_instance_of(::Trax::Model::UUIDPrefix)
7
+ }
8
+
5
9
  describe "uuid_prefix" do
6
10
  context "bad prefixes" do
7
11
  ["a", "1p", "a1a", "bl", "1", "111"].each do |prefix|
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ describe ::Trax::Model::UUIDPrefix do
3
+ subject{ Trax::Model::UUIDPrefix.new('1a') }
4
+
5
+ describe "#to_i" do
6
+ {"1a" => 10, "2a" => 20, "9e" => 94, "8d" => 83, "0a" => 0, "0b" => 1, "f9" => 251}.each_pair do |prefix_string, assertion|
7
+ it "#{prefix_string} should eq #{assertion}" do
8
+ prefix = ::Trax::Model::UUIDPrefix.new(prefix_string)
9
+
10
+ prefix.to_i.should eq assertion
11
+ end
12
+ end
13
+ end
14
+
15
+ describe ".all" do
16
+ it { described_class.all.should include("9a") }
17
+ it { described_class.all.length.should eq 120 }
18
+ end
19
+
20
+ describe ".build_from_integer" do
21
+ it { described_class.build_from_integer(10).should eq "1a" }
22
+ end
23
+
24
+ describe "#next" do
25
+ let(:test_subject) { Product.uuid_prefix }
26
+
27
+ it { test_subject.next.should eq '1b' }
28
+
29
+ context "lower to higher register transitions" do
30
+ let(:test_subject) { ::Trax::Model::UUIDPrefix.new('9f') }
31
+ it { test_subject.next.should eq 'a0'}
32
+ end
33
+ end
34
+
35
+ describe "#previous" do
36
+ let(:test_subject) { Product.uuid_prefix }
37
+
38
+ it { test_subject.previous.should eq '0f' }
39
+ end
40
+
41
+ describe "#to_i" do
42
+ context "when prefix in {int,str} order" do
43
+ let(:test_subject) { Product.uuid_prefix }
44
+
45
+ it { test_subject.to_i.should eq 10 }
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trax_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Ayre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: trax_core
@@ -271,6 +271,7 @@ files:
271
271
  - lib/trax/model/registry.rb
272
272
  - lib/trax/model/unique_id.rb
273
273
  - lib/trax/model/uuid.rb
274
+ - lib/trax/model/uuid_prefix.rb
274
275
  - lib/trax/model/validators.rb
275
276
  - lib/trax/string.rb
276
277
  - lib/trax/validators/email_validator.rb
@@ -287,6 +288,7 @@ files:
287
288
  - spec/trax/model/matchable_spec.rb
288
289
  - spec/trax/model/registry_spec.rb
289
290
  - spec/trax/model/unique_id_spec.rb
291
+ - spec/trax/model/uuid_prefix_spec.rb
290
292
  - spec/trax/model/uuid_spec.rb
291
293
  - spec/trax/model_spec.rb
292
294
  - spec/trax/string_spec.rb
@@ -327,6 +329,7 @@ test_files:
327
329
  - spec/trax/model/matchable_spec.rb
328
330
  - spec/trax/model/registry_spec.rb
329
331
  - spec/trax/model/unique_id_spec.rb
332
+ - spec/trax/model/uuid_prefix_spec.rb
330
333
  - spec/trax/model/uuid_spec.rb
331
334
  - spec/trax/model_spec.rb
332
335
  - spec/trax/string_spec.rb