Class: Ygg::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/ygg/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = nil) ⇒ Session

Returns a new instance of Session.



3
4
5
6
7
8
# File 'lib/ygg/session.rb', line 3

def initialize(source = nil)
  if source
    @source = source
    @records = DataBundle.new load
  end
end

Instance Method Details

#has_loaded_some?(of_class) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


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

def has_loaded_some?(of_class)
  raise ArgumentError, "Not a class" unless of_class.is_a? Class
  raise NoInteractionDoneException, 
    "No interaction done yet." unless records
  records.each do |record|
    return true if record.is_a? of_class
  end
  return false
end

#load(path = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ygg/session.rb', line 10

def load(path = nil)
  @source = path if path
  begin
    File.exists?(@source) ? YAML.load_file(@source) : []
  rescue Psych::SyntaxError => syntax_error
    raise InvalidResourceException,
      "The resource on #{@source} is invalid"
  end
end

#recordsObject



32
# File 'lib/ygg/session.rb', line 32

def records; @records; end

#shutdownObject



20
21
22
23
24
# File 'lib/ygg/session.rb', line 20

def shutdown
  File.open @source, "w" do |file|
    file.write @records.to_yaml
  end
end

#sourceObject



26
27
28
29
# File 'lib/ygg/session.rb', line 26

def source
  raise Ygg::UnavailableResourceException, "No resource was setted." unless @source
  @source
end

#source=(source) ⇒ Object



31
# File 'lib/ygg/session.rb', line 31

def source=(source); @source = source; end