Class: Resource

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Validation
Defined in:
lib/generators/rcap/models/templates/models/resource.rb

Overview

A Resourse object is valid if

  • it has a resource description

Constant Summary collapse

XML_ELEMENT_NAME =

:nodoc:

'resource'
MIME_TYPE_ELEMENT_NAME =

:nodoc:

'mimeType'
SIZE_ELEMENT_NAME =

:nodoc:

'size'
URI_ELEMENT_NAME =

:nodoc:

'uri'
DEREF_URI_ELEMENT_NAME =

:nodoc:

'derefUri'
DIGEST_ELEMENT_NAME =

:nodoc:

'digest'
RESOURCE_DESC_ELEMENT_NAME =

:nodoc:

'resourceDesc'
XPATH =

:nodoc:

"cap:#{ XML_ELEMENT_NAME }"
MIME_TYPE_XPATH =

:nodoc:

"cap:#{ MIME_TYPE_ELEMENT_NAME }"
SIZE_XPATH =

:nodoc:

"cap:#{ SIZE_ELEMENT_NAME }"
URI_XPATH =

:nodoc:

"cap:#{ URI_ELEMENT_NAME }"
DEREF_URI_XPATH =

:nodoc:

"cap:#{ DEREF_URI_ELEMENT_NAME }"
DIGEST_XPATH =

:nodoc:

"cap:#{ DIGEST_ELEMENT_NAME }"
RESOURCE_DESC_XPATH =

:nodoc:

"cap:#{ RESOURCE_DESC_ELEMENT_NAME }"
RESOURCE_DESC_YAML =

:nodoc:

"Resource Description"
URI_YAML =

:nodoc:

"URI"
MIME_TYPE_YAML =

:nodoc:

"Mime Type"
DEREF_URI_YAML =

:nodoc:

"Derefrenced URI Data"
SIZE_YAML =

:nodoc:

"Size"
DIGEST_YAML =

:nodoc:

"Digest"
RESOURCE_DESC_KEY =

:nodoc:

'resource_desc'
URI_KEY =

:nodoc:

'uri'
MIME_TYPE_KEY =

:nodoc:

'mime_type'
DEREF_URI_KEY =

:nodoc:

'deref_uri'
SIZE_KEY =

:nodoc:

'size'
DIGEST_KEY =

:nodoc:

'digest'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Resource

Returns a new instance of Resource.



38
39
40
41
42
43
44
45
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 38

def initialize( attributes = {} )
  @mime_type     = attributes[ :mime_type ]
  @size          = attributes[ :size ]
  @uri           = attributes[ :uri ]
  @deref_uri     = attributes[ :deref_uri ]
  @digest        = attributes[ :digest ]
  @resource_desc = attributes[ :resource_desc ]
end

Instance Attribute Details

#deref_uriObject

Dereferenced URI - contents of URI Base64 encoded



16
17
18
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 16

def deref_uri
  @deref_uri
end

#digestObject

SHA-1 hash of contents of resource



18
19
20
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 18

def digest
  @digest
end

#mime_typeObject

Returns the value of attribute mime_type.



10
11
12
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 10

def mime_type
  @mime_type
end

#resource_descObject

Resource Description



9
10
11
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 9

def resource_desc
  @resource_desc
end

#sizeObject

Expressed in bytes



12
13
14
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 12

def size
  @size
end

#uriObject

Resource location



14
15
16
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 14

def uri
  @uri
end

Class Method Details

.from_h(resource_hash) ⇒ Object

:nodoc:



134
135
136
137
138
139
140
141
142
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 134

def self.from_h( resource_hash ) # :nodoc:
  self.new(
    :resource_desc => resource_hash[ RESOURCE_DESC_KEY ],
    :uri => resource_hash[ URI_KEY ],
    :mime_type => resource_hash[ MIME_TYPE_KEY ],
    :deref_uri => resource_hash[ DEREF_URI_KEY ],
    :size => resource_hash[ SIZE_KEY ],
    :digest => resource_hash[ DIGEST_KEY ])
end

.from_xml_element(resource_xml_element) ⇒ Object

:nodoc:



79
80
81
82
83
84
85
86
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 79

def self.from_xml_element( resource_xml_element ) # :nodoc:
  resource = self.new( :resource_desc => RCAP.xpath_text( resource_xml_element, RESOURCE_DESC_XPATH ),
                      :uri           => RCAP.xpath_text( resource_xml_element, URI_XPATH ),
                      :mime_type     => RCAP.xpath_text( resource_xml_element, MIME_TYPE_XPATH ),
                      :deref_uri     => RCAP.xpath_text( resource_xml_element, DEREF_URI_XPATH ),
                      :size          => RCAP.xpath_text( resource_xml_element, SIZE_XPATH ),
                      :digest        => RCAP.xpath_text( resource_xml_element, DIGEST_XPATH ))
end

.from_yaml_data(resource_yaml_data) ⇒ Object

:nodoc:



106
107
108
109
110
111
112
113
114
115
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 106

def self.from_yaml_data( resource_yaml_data ) # :nodoc:
  self.new(
    :resource_desc => reource_yaml_data[ RESOURCE_DESC_YAML ],
    :uri           => reource_yaml_data[ URI_YAML ],
    :mime_type     => reource_yaml_data[ MIME_TYPE_YAML ],
    :deref_uri     => reource_yaml_data[ DEREF_URI_YAML ],
    :size          => reource_yaml_data[ SIZE_YAML ],
    :digest        => reource_yaml_data[ DIGEST_YAML ]
  )
end

Instance Method Details

#inspectObject

:nodoc:



69
70
71
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 69

def inspect # :nodoc:
  [ self.resource_desc, self.uri, self.mime_type, self.size ? format( "%.1fKB", self.size_in_kb ) : nil ].compact.join(' - ')
end

#size_in_kbObject

If size is defined returns the size in kilobytes



59
60
61
62
63
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 59

def size_in_kb
  if self.size
    self.size.to_f/1024
  end
end

#to_hObject

:nodoc:



124
125
126
127
128
129
130
131
132
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 124

def to_h # :nodoc:
  RCAP.attribute_values_to_hash(
    [ RESOURCE_DESC_KEY, self.resource_desc ],
    [ URI_KEY,           self.uri],
    [ MIME_TYPE_KEY,     self.mime_type],
    [ DEREF_URI_KEY,     self.deref_uri],
    [ SIZE_KEY,          self.size ],
    [ DIGEST_KEY,        self.digest ])
end

#to_sObject

Returns a string representation of the resource of the form

resource_desc


75
76
77
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 75

def to_s
  self.resource_desc
end

#to_xmlObject

:nodoc:



65
66
67
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 65

def to_xml # :nodoc:
  self.to_xml_element.to_s
end

#to_xml_elementObject

:nodoc:



47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 47

def to_xml_element # :nodoc:
  xml_element = REXML::Element.new( XML_ELEMENT_NAME )
  xml_element.add_element( RESOURCE_DESC_ELEMENT_NAME ).add_text( self.resource_desc )
  xml_element.add_element( MIME_TYPE_ELEMENT_NAME ).add_text( self.mime_type ) if self.mime_type
  xml_element.add_element( SIZE_ELEMENT_NAME ).add_text( self.size )           if self.size
  xml_element.add_element( URI_ELEMENT_NAME ).add_text( self.uri )             if self.uri
  xml_element.add_element( DEREF_URI_ELEMENT_NAME ).add_text( self.deref_uri ) if self.deref_uri
  xml_element.add_element( DIGEST_ELEMENT_NAME ).add_text( self.digest )       if self.digest
  xml_element
end

#to_yaml(options) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/rcap/models/templates/models/resource.rb', line 95

def to_yaml( options ) # :nodoc:
  RCAP.attribute_values_to_hash(
    [ RESOURCE_DESC_YAML, self.resource_desc ],
    [ URI_YAML,           self.uri ],
    [ MIME_TYPE_YAML,     self.mime_type ],
    [ DEREF_URI_YAML,     self.deref_uri ],
    [ SIZE_YAML,          self.size ],
    [ DIGEST_YAML,        self.digest ]
  ).to_yaml( options )
end