Class: Lims::Core::Persistence::UuidResource

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/lims-core/persistence/uuid_resource.rb,
lib/lims-core/persistence/uuid_resource_persistor.rb

Overview

Bind a uuid (as a String) to a Resource (a key and a model) The key is a FixNum to find the corresponding resource in the store and the model is the real class of the object (or at least something allowing to find it) the ‘state` attribute is the ResourceState corresponding to linked Resource.

Defined Under Namespace

Classes: InvalidUuidError, UuidResourcePersistor

Constant Summary collapse

Generator =
UUID.new
Form =
[8, 4, 4, 4, 12]
Length =
Form.inject { |m, n| m+n }
ValidationRegexp =
/#{Form.map { |n| "[0-9a-f]{#{n}}" }.join('-')}/i
SplitRegexp =
/#{Form.map { |n| "([0-9a-f]{#{n}})"}.join('')}/

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

included, tracker_included

Methods included from SubclassTracker

extended

Constructor Details

#initialize(*args) ⇒ UuidResource

Returns a new instance of UuidResource.



20
21
22
# File 'lib/lims-core/persistence/uuid_resource.rb', line 20

def initialize(*args)
  super(*args)
end

Class Method Details

.bignum_to_string(b) ⇒ String

Convert a bignum to a string representation

Parameters:

  • b (Bignum)

Returns:

  • (String)


89
90
91
92
# File 'lib/lims-core/persistence/uuid_resource.rb', line 89

def self.bignum_to_string(b)
  l = Form.inject { |m, n| m+n }
  expand("%0.*x" % [l,b])
end

.compact(s) ⇒ Object



100
101
102
# File 'lib/lims-core/persistence/uuid_resource.rb', line 100

def self.compact(s)
  s.tr('-', '')
end

.expand(s) ⇒ Object

Raises:



94
95
96
97
98
# File 'lib/lims-core/persistence/uuid_resource.rb', line 94

def self.expand(s)
  match = SplitRegexp.match(s)
  raise InvalidUuidError.new(s) unless match
  match.captures.join('-')
end

.generate_uuidObject



66
67
68
# File 'lib/lims-core/persistence/uuid_resource.rb', line 66

def self.generate_uuid()
  Generator.generate
end

.pack(to_pack) ⇒ Object

Pack a string representation of an uuid to a char(16)



71
72
73
# File 'lib/lims-core/persistence/uuid_resource.rb', line 71

def self.pack(to_pack)
  [compact(to_pack)].pack("H*")
end

.string_to_bignum(s) ⇒ Bignum

Convert the string representation of an Uuid to a bignum

Parameters:

  • s (String)

Returns:

  • (Bignum)


82
83
84
# File 'lib/lims-core/persistence/uuid_resource.rb', line 82

def self.string_to_bignum(s)
  compact(s).to_i(16)
end

.unpack(packed) ⇒ Object



75
76
77
# File 'lib/lims-core/persistence/uuid_resource.rb', line 75

def self.unpack(packed)
  expand(packed.unpack("H*").first)
end

.valid?(uuid) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/lims-core/persistence/uuid_resource.rb', line 62

def self.valid?(uuid)
  !!(uuid =~ ValidationRegexp)
end

Instance Method Details

#attributesObject

for speed



33
34
35
36
37
38
# File 'lib/lims-core/persistence/uuid_resource.rb', line 33

def attributes
  {state: state,
    model_class: model_class,
    uuid: uuid
  }
end

#attributes_for_dirtyObject



40
41
42
43
44
45
46
# File 'lib/lims-core/persistence/uuid_resource.rb', line 40

def attributes_for_dirty
  {state: state,
    model_class: model_class,
  }.tap do |att|
    att[:uuid] = @uuid if @uuid
  end
end

#keyObject



58
59
60
# File 'lib/lims-core/persistence/uuid_resource.rb', line 58

def key
  @state.andtap { |state|  state.id }
end

#state=(state_) ⇒ Object

Link the resouce state to the UuidResource itself



25
26
27
28
29
30
# File 'lib/lims-core/persistence/uuid_resource.rb', line 25

def state=(state_)
  @state = state_
  if state_
    state_.uuid_resource = self
  end
end

#uuidObject



109
110
111
# File 'lib/lims-core/persistence/uuid_resource.rb', line 109

def uuid
  @uuid ||= UuidResource.generate_uuid
end