Class: OMF::SFA::Resource::GURN

Inherits:
Object
  • Object
show all
Defined in:
lib/omf-sfa/resource/gurn.rb

Overview

< OMF::Base::MObject

Constant Summary collapse

@@def_domain =
"urn:publicid:IDN+acme.org"
@@name2obj =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(short_name, type = nil, domain = nil) ⇒ GURN

Returns a new instance of GURN.



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/omf-sfa/resource/gurn.rb', line 89

def initialize(short_name, type = nil, domain = nil)
  @short_name = short_name
  @domain = domain || @@def_domain
  if type
    @type =  type
    @name = "#{@domain}+#{type}+#{short_name}"
  else
    @name = "#{@domain}+#{short_name}"
  end
  @urn = 'urn:publicid:IDN+' + name
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



87
88
89
# File 'lib/omf-sfa/resource/gurn.rb', line 87

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



87
88
89
# File 'lib/omf-sfa/resource/gurn.rb', line 87

def name
  @name
end

#short_nameObject (readonly)

Returns the value of attribute short_name.



87
88
89
# File 'lib/omf-sfa/resource/gurn.rb', line 87

def short_name
  @short_name
end

#typeObject (readonly)

Returns the value of attribute type.



87
88
89
# File 'lib/omf-sfa/resource/gurn.rb', line 87

def type
  @type
end

#urnObject (readonly)

Returns the value of attribute urn.



87
88
89
# File 'lib/omf-sfa/resource/gurn.rb', line 87

def urn
  @urn
end

Class Method Details

.clear_cacheObject

This class maintains a cache between object name and it’s GURN. As this may get in the way of testing, this method provides a way of clear that cache



83
84
85
# File 'lib/omf-sfa/resource/gurn.rb', line 83

def self.clear_cache
  @@name2obj.clear
end

.create(name, opts = {}) ⇒ Object

Create a GURN

Parameters:

  • name (String)

    Name of GURN

  • opts (Hash) (defaults to: {})

    options to further describe GURN components

Options Hash (opts):

  • :type (String)

    GURN’s type

  • :model (Class)

    Class responding to either :sfa_class or :urn_type

  • :domain (String)

    GURN’s domain



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omf-sfa/resource/gurn.rb', line 21

def self.create(name, opts = {})
  return name if name.kind_of? self
  unless name
    raise GurnMalformedException.new "No name given"
  end
  #puts "GUID: #{name}###{opts}"

  obj = @@name2obj[name]
  return obj if obj

  if name.start_with?('urn')
    return parse(name)
  end

  unless type = opts[:type]
    model = opts[:model]
    if model && model.respond_to?(:sfa_class)
      type =  model.sfa_class
    elsif model && model.respond_to?(:resource_type)
      type =  model.resource_type
    end
  end
  domain = opts[:domain] || @@def_domain
  return @@name2obj[name] = self.new(name, type, domain)
end

.default_domainObject



75
76
77
# File 'lib/omf-sfa/resource/gurn.rb', line 75

def self.default_domain()
  @@def_domain
end

.default_domain=(domain) ⇒ Object



71
72
73
# File 'lib/omf-sfa/resource/gurn.rb', line 71

def self.default_domain=(domain)
  @@def_domain = domain
end

.parse(urn_str) ⇒ Object

Create a GURN object from urn_str.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/omf-sfa/resource/gurn.rb', line 53

def self.parse(urn_str)
  if urn_str.start_with? 'urn:publicid:IDN'
    a = urn_str.split('+')
    a.delete_at(0) # get rid of "urn:publicid:IDN"
    if a.length == 3
      prefix, type, name = a
    elsif a.length == 2
      prefix, name = a
      type = nil
    else
      raise GurnMalformedException.new "unknown format '#{urn_str}' for GURN (#{a.inspect})."
    end
    @@name2obj[urn_str] = self.new(name, type, prefix)
  else
    raise GurnMalformedException.new "unknown format '#{urn_str}' for GURN - expected it to start with 'urn:publicid:IDN'."
  end
end

.sfa_create(name, context = nil) ⇒ Object



47
48
49
# File 'lib/omf-sfa/resource/gurn.rb', line 47

def self.sfa_create(name, context = nil)
  return create(name, :model => context)
end

Instance Method Details

#to_sObject



119
120
121
# File 'lib/omf-sfa/resource/gurn.rb', line 119

def to_s
  @urn
end

#uuidObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/omf-sfa/resource/gurn.rb', line 101

def uuid
  unless @uuid
    begin
      @uuid = UUIDTools::UUID.parse(short_name)
    rescue ArgumentError
      if (p = short_name.split(':')).length > 1
        # ExoGeni has short names of the form uuid:short_name
        # TODO: This turned out to be not a sliver UUID, but something else
        # begin
          # @uuid = UUIDTools::UUID.parse(p[0])
        # rescue ArgumentError
        # end
      end
    end
  end
  @uuid
end