Class: Key

Inherits:
RestApi::Base show all
Defined in:
app/models/key.rb

Overview

The REST API model object representing an SSH public key.

Defined Under Namespace

Classes: DuplicateName

Constant Summary collapse

Inf =

Replace with Float::INFINITY in 1.9 ruby

1.0/0.0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RestApi::Base

alias_attribute, aliased_attributes, allow_anonymous?, #as, #as=, #assign_attributes, attr_alters, #attributes=, calculated_attributes, #clone, configuration=, connection, custom_id, delete, #dup, #duplicate_errors, element_path, exception_for_code, find, find_one, get, #get, #has_exit_code?, headers, #initialize, #load, #load_remote_errors, on_exit_code, #raise_on_invalid, #reload, remote_errors_for, #remote_results, #save!, #save_with_change_tracking, shared_connection, singleton?, #to_json, translate_api_error, use_patch_on_update?, #valid?

Methods included from ActiveResource::Associations

#belongs_to, #has_many, #has_one

Constructor Details

This class inherits a constructor from RestApi::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RestApi::Base

Class Method Details

.default(options = nil) ⇒ Object



83
84
85
# File 'app/models/key.rb', line 83

def default(options=nil)
  Key.find('default', options)
end

Instance Method Details

#default?Boolean

Until the empty default key is attached from domain, ignore in lists

Returns:

  • (Boolean)


75
76
77
# File 'app/models/key.rb', line 75

def default?
  'default' == name
end

#display_contentObject



64
65
66
67
68
69
70
# File 'app/models/key.rb', line 64

def display_content
  if content.length > 20
    "#{content[1..8]}..#{content[-8..-1]}"
  else
    content
  end
end

#empty_default?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/key.rb', line 78

def empty_default?
  default? and (!content or content.blank? or 'nossh' == content)
end

#idObject

support ID for legacy code which retrieves keys by .id



15
16
17
# File 'app/models/key.rb', line 15

def id # support ID for legacy code which retrieves keys by .id
  name
end

#make_unique!(format = 'key %s') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/key.rb', line 52

def make_unique!(format='key %s')
  unless persisted?
    keys = Key.find(:all, :as => as)
    if keys.any? {|k| k.name == name }
      self.name = format % (2..keys.length+2).find do |i|
        not keys.any? {|k| k.name == format % i}
      end
    end
  end
  self
end

#raw_content=(contents) ⇒ Object

Raw content is decomposed into content and type



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/key.rb', line 29

def raw_content=(contents)
  if contents
    parts = contents.split
    case parts.length
    when 0
      self.type = self.content = nil
    when 1
      self.type = nil
      self.content = parts[0]
    else
      self.type, self.content = parts
    end
  end
  super
end

#typeObject

type is a method on Object in 1.8.7 and must be overriden so that the attribute will be read (method_missing is how ActiveResource attributes are looked up)



21
22
23
# File 'app/models/key.rb', line 21

def type
  @attributes[:type]
end