Sha256: 1ae14bb8faac3d59af9fec8c258785c61d99bcbe0f4d9f50251cc5eef1b06507

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 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
          groups = send(@load_method, resource.to_s)
          groups[action.to_s] || groups["defaults"] || []
        else
          []
        end
      end

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

      def map_of_all
        result = {}
        Dir[File.join(@guards_dir, "*_guard.yml")].each do |file|
          result.merge!(YAML.load_file(file))
        end
        result
      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

5 entries across 5 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