Class: VersionOne::AssetRef

Inherits:
Object
  • Object
show all
Defined in:
lib/version-one/asset_ref.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_or_asset) ⇒ AssetRef

Returns a new instance of AssetRef.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/version-one/asset_ref.rb', line 7

def initialize(xml_or_asset)
  case xml_or_asset
    when Asset
      @asset = xml_or_asset
      @id = @asset.id
      @href = @asset.href
    when Hash
      @id = xml_or_asset[:id]
      @href = xml_or_asset[:href]
    else
      @href = xml_or_asset.attributes['href']
      @id = xml_or_asset.attributes['idref']
  end
  raise ArgumentError, "Could not get id and href" unless @id || @href
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



53
54
55
# File 'lib/version-one/asset_ref.rb', line 53

def method_missing(method, *args, &block)
  get.send(method, *args, &block)
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



5
6
7
# File 'lib/version-one/asset_ref.rb', line 5

def href
  @href
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/version-one/asset_ref.rb', line 5

def id
  @id
end

Class Method Details

.for(x) ⇒ Object



23
24
25
# File 'lib/version-one/asset_ref.rb', line 23

def self.for(x)
  x.is_a?(AssetRef) ? x : AssetRef.new(x)
end

Instance Method Details

#get(*fields) ⇒ Object



27
28
29
# File 'lib/version-one/asset_ref.rb', line 27

def get(*fields)
  @asset ||= get_asset(*fields)
end

#inspectObject



42
43
44
# File 'lib/version-one/asset_ref.rb', line 42

def inspect
  "#<AssetRef:#{@href}>"
end

#set(asset) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/version-one/asset_ref.rb', line 31

def set(asset)
  case
    when !asset.is_a?(VersionOne::Asset)
      raise ArgumentError, "Parameter must be a VersionOne asset"
    when asset.id == @id
      @asset = asset
    else
      raise ArgumentError, "Asset does not match reference"
  end
end

#to_xmlObject



46
47
48
49
50
51
# File 'lib/version-one/asset_ref.rb', line 46

def to_xml
  xml = XML::Node.new('Asset')
  xml.attributes['href'] = @href if @href
  xml.attributes['idref'] = @id if @id
  xml
end