Class: CanTango::PermissionEngine::YamlStore

Inherits:
Store
  • Object
show all
Defined in:
lib/cantango/permission_engine/yaml_store.rb

Instance Attribute Summary collapse

Attributes inherited from Store

#name, #options

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ YamlStore

for a YamlStore, the name is the name of the yml file options: extension, path



11
12
13
# File 'lib/cantango/permission_engine/yaml_store.rb', line 11

def initialize name, options = {}
  super
end

Instance Attribute Details

#last_load_timeObject (readonly)

Returns the value of attribute last_load_time.



6
7
8
# File 'lib/cantango/permission_engine/yaml_store.rb', line 6

def last_load_time
  @last_load_time
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/cantango/permission_engine/yaml_store.rb', line 6

def path
  @path
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/cantango/permission_engine/yaml_store.rb', line 31

def changed?
  return true if !last_load_time
  last_modify_time <= last_load_time
end

#last_modify_timeObject



36
37
38
# File 'lib/cantango/permission_engine/yaml_store.rb', line 36

def last_modify_time
  File.mtime(file_path)
end

#load!Object



15
16
17
18
# File 'lib/cantango/permission_engine/yaml_store.rb', line 15

def load!
  loader.load!
  @last_load_time = Time.now
end

#load_from_hash(hash) ⇒ Object



20
21
22
# File 'lib/cantango/permission_engine/yaml_store.rb', line 20

def load_from_hash hash
  loader.load_from_hash hash
end

#permissionsObject

return cached permissions if file has not changed since last load otherwise load permissions again to reflect changes!



26
27
28
29
# File 'lib/cantango/permission_engine/yaml_store.rb', line 26

def permissions
  return @permissions if changed?
  @permissions = loader.permissions
end

#save!(perms = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/cantango/permission_engine/yaml_store.rb', line 64

def save! perms = nil
  save_permissions(perms) if perms

  File.open(file_path, 'w') do |f|
    f.write to_yaml
  end
end

#save_permissions(perms) ⇒ Object



72
73
74
# File 'lib/cantango/permission_engine/yaml_store.rb', line 72

def save_permissions perms
  load_from_hash perms
end