Sha256: e88b29b302fffc14fe2c66393da7f654596d8d9b1717143bffe1c77d40299cf5

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

require 'yaml'
module Ixtlan
  module Guard
    class Config

      def initialize(options = {})
        @guards_dir = options[:guards_dir]
        @load_method = options[:cache] ? :cached_load_from_yaml_file : :load_from_yaml_file
        raise GuardException.new("guards directory does not exists: #{@guards_dir}") unless File.directory?(@guards_dir)
      end

      def allowed_groups(resource, action)
        if resource && action
          resource = resource.to_s
          groups = send(@load_method, resource)
          groups[action.to_s] || groups["defaults"] || []
        else
          []
        end
      end

      def has_guard?(resource)
        File.exists? yaml_file(resource)
      end

      private
      
      def cached_load_from_yaml_file(resource)
        @cache ||= {}
        @cache[resource] ||= load_from_yaml_file(resource)
      end

      def yaml_file(resource)
        File.join(@guards_dir, "#{resource}_guard.yml")
      end

      def load_from_yaml_file(resource)
        file = yaml_file(resource)
        if File.exists? file
          YAML.load_file(file)[resource] || {}
        else
          {}
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ixtlan-guard-0.9.0 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.8.3 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.8.2 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.8.1 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.8.0 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.7.2 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.7.0 lib/ixtlan/guard/guard_config.rb~
ixtlan-guard-0.6.1 lib/ixtlan/guard/guard_config.rb~