class VersionOne::AssetRef

Attributes

href[R]
id[R]

Public Class Methods

for(x) click to toggle source
# File lib/version-one/asset_ref.rb, line 23
def self.for(x)
  x.is_a?(AssetRef) ? x : AssetRef.new(x)
end
new(xml_or_asset) click to toggle source
# 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

Public Instance Methods

get(*fields) click to toggle source
# File lib/version-one/asset_ref.rb, line 27
def get(*fields)
  @asset ||= get_asset(*fields)
end
inspect() click to toggle source
# File lib/version-one/asset_ref.rb, line 42
def inspect
  "#<AssetRef:#{@href}>"
end
method_missing(method, *args, &block) click to toggle source
# File lib/version-one/asset_ref.rb, line 53
def method_missing(method, *args, &block)
  get.send(method, *args, &block)
end
set(asset) click to toggle source
# 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_xml() click to toggle source
# 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

Private Instance Methods

get_asset(*fields) click to toggle source
# File lib/version-one/asset_ref.rb, line 59
def get_asset(*fields)
  client = VersionOne::Client.new
  xml = client.get(href, *fields)
  Asset.new(xml: xml)
end