Class: Keyrack::Site

Inherits:
Hash
  • Object
show all
Defined in:
lib/keyrack/site.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Site

Returns a new instance of Site.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/keyrack/site.rb', line 3

def initialize(*args)
  if args[0].is_a?(Hash)
    hash = args[0]
    if !hash.has_key?('name')
      raise ArgumentError, "hash is missing the 'name' key"
    end
    if !hash['name'].is_a?(String)
      raise ArgumentError, "name is not a String"
    end
    if !hash.has_key?('username')
      raise ArgumentError, "hash is missing the 'username' key"
    end
    if !hash['username'].is_a?(String)
      raise ArgumentError, "name is not a String"
    end
    if !hash.has_key?('password')
      raise ArgumentError, "hash is missing the 'password' key"
    end
    if !hash['password'].is_a?(String)
      raise ArgumentError, "name is not a String"
    end
    self.update(hash)
  else
    self['name'] = args[0]
    self['username'] = args[1]
    self['password'] = args[2]
  end

  @event_hooks = []
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/keyrack/site.rb', line 76

def ==(other)
  if other.instance_of?(Site)
    other.name == name && other.username == username
  else
    super
  end
end

#after_event(&block) ⇒ Object



68
69
70
# File 'lib/keyrack/site.rb', line 68

def after_event(&block)
  @event_hooks << block
end

#change_attribute(name, value) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/keyrack/site.rb', line 34

def change_attribute(name, value)
  event = Event.new(self, 'change')
  event.attribute_name = name
  event.previous_value = self[name]
  event.new_value = value

  self[name] = value
  trigger(event)
end

#encode_with(coder) ⇒ Object



72
73
74
# File 'lib/keyrack/site.rb', line 72

def encode_with(coder)
  coder.represent_map(nil, self)
end

#nameObject



44
45
46
# File 'lib/keyrack/site.rb', line 44

def name
  self['name']
end

#name=(name) ⇒ Object



48
49
50
# File 'lib/keyrack/site.rb', line 48

def name=(name)
  change_attribute('name', name)
end

#passwordObject



60
61
62
# File 'lib/keyrack/site.rb', line 60

def password
  self['password']
end

#password=(password) ⇒ Object



64
65
66
# File 'lib/keyrack/site.rb', line 64

def password=(password)
  change_attribute('password', password)
end

#usernameObject



52
53
54
# File 'lib/keyrack/site.rb', line 52

def username
  self['username']
end

#username=(username) ⇒ Object



56
57
58
# File 'lib/keyrack/site.rb', line 56

def username=(username)
  change_attribute('username', username)
end