Class: Keyrack::Site

Inherits:
Object
  • 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
33
34
# 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
  else
    hash = {
      'name' => args[0],
      'username' => args[1],
      'password' => args[2]
    }
  end
  @attributes = hash

  @event_hooks = []
end

Instance Method Details

#==(other) ⇒ Object



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

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

#after_event(&block) ⇒ Object



70
71
72
# File 'lib/keyrack/site.rb', line 70

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

#change_attribute(name, value) ⇒ Object



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

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

  @attributes[name] = value
  trigger(event)
end

#nameObject



46
47
48
# File 'lib/keyrack/site.rb', line 46

def name
  @attributes['name']
end

#name=(name) ⇒ Object



50
51
52
# File 'lib/keyrack/site.rb', line 50

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

#passwordObject



62
63
64
# File 'lib/keyrack/site.rb', line 62

def password
  @attributes['password']
end

#password=(password) ⇒ Object



66
67
68
# File 'lib/keyrack/site.rb', line 66

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

#to_hObject



82
83
84
# File 'lib/keyrack/site.rb', line 82

def to_h
  @attributes.clone
end

#usernameObject



54
55
56
# File 'lib/keyrack/site.rb', line 54

def username
  @attributes['username']
end

#username=(username) ⇒ Object



58
59
60
# File 'lib/keyrack/site.rb', line 58

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