Class: WAZ::Tables::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/waz/tables/table.rb

Overview

# delete table my_table.destroy!

# create a new table WAZ::Tables::Table.create(‘new-table’)

Constant Summary collapse

INVALID_TABLE_ERROR_MESSAGE =
"must start with at least one lower/upper characted, can have character or any digit starting from the second position, must be from 3 through 63 characters long"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



61
62
63
64
65
66
67
# File 'lib/waz/tables/table.rb', line 61

def initialize(options = {})
  raise WAZ::Storage::InvalidOption, :name unless options.keys.include?(:name) and !options[:name].empty?
  raise WAZ::Storage::InvalidOption, :url unless options.keys.include?(:url) and !options[:url].empty?        
  raise WAZ::Storage::InvalidParameterValue, {:name => options[:name], :values => [INVALID_TABLE_ERROR_MESSAGE]} unless WAZ::Storage::ValidationRules.valid_table_name?(options[:name])        
  self.name = options[:name]
  self.url = options[:url]        
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



59
60
61
# File 'lib/waz/tables/table.rb', line 59

def name
  @name
end

#urlObject

Returns the value of attribute url.



59
60
61
# File 'lib/waz/tables/table.rb', line 59

def url
  @url
end

Class Method Details

.create(table_name) ⇒ Object

Creates a table on the current account.



45
46
47
48
# File 'lib/waz/tables/table.rb', line 45

def create(table_name)
  raise WAZ::Storage::InvalidParameterValue, {:name => table_name, :values => [INVALID_TABLE_ERROR_MESSAGE]} unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
  WAZ::Tables::Table.new(service_instance.create_table(table_name))
end

.find(table_name) ⇒ Object

Finds a table by name. It will return nil if no table was found.



26
27
28
29
30
31
32
33
# File 'lib/waz/tables/table.rb', line 26

def find(table_name)
  raise WAZ::Storage::InvalidParameterValue, {:name => table_name, :values => [INVALID_TABLE_ERROR_MESSAGE]} unless WAZ::Storage::ValidationRules.valid_table_name?(table_name)
  begin 
    WAZ::Tables::Table.new(service_instance.get_table(table_name))
  rescue WAZ::Tables::TableDoesNotExist
    return nil
  end
end

.list(continuation_token = {}) ⇒ Object

Returns an array of the existing tables (WAZ::Tables::Table) on the current Windows Azure Storage account.



37
38
39
40
41
42
# File 'lib/waz/tables/table.rb', line 37

def list(continuation_token = {})
    table_list, next_table_name = service_instance.list_tables(continuation_token['NextTableName'])
    tables = TableArray.new(table_list.map { |table| WAZ::Tables::Table.new({ :name => table[:name], :url => table[:url] }) })
    tables.continuation_token = {'NextTableName' => next_table_name} unless next_table_name.nil?
    return tables
end

.service_instanceObject

This method is internally used by this class. It’s the way we keep a single instance of the service that wraps the calls the Windows Azure Tables API. It’s initialized with the values from the default_connection on WAZ::Storage::Base initialized thru establish_connection!



53
54
55
56
# File 'lib/waz/tables/table.rb', line 53

def service_instance
  options = WAZ::Storage::Base.default_connection.merge(:type_of_service => "table")
  (@service_instances ||= {})[options[:account_name]] ||= Service.new(options)
end

Instance Method Details

#destroy!Object

Removes the table from the current account.



70
71
72
# File 'lib/waz/tables/table.rb', line 70

def destroy!
  self.class.service_instance.delete_table(self.name)
end