File: rule.rb

package info (click to toggle)
ruby-licensee 9.15.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,244 kB
  • sloc: xml: 10,130; ruby: 5,012; makefile: 9
file content (54 lines) | stat: -rw-r--r-- 1,279 bytes parent folder | download
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Licensee
  class Rule
    attr_reader :tag, :label, :description, :group

    include Licensee::HashHelper
    HASH_METHODS = %i[tag label description].freeze

    def initialize(tag: nil, label: nil, description: nil, group: nil)
      @tag = tag
      @label = label
      @description = description
      @group = group
    end

    def inspect
      "#<Licensee::Rule @tag=\"#{tag}\">"
    end

    class << self
      def all
        @all ||= raw_rules.map do |group, rules|
          rules.map do |rule|
            Rule.new(
              tag:         rule['tag'],
              label:       rule['label'],
              description: rule['description'],
              group:       group
            )
          end
        end.flatten
      end

      def find_by_tag_and_group(tag, group = nil)
        Rule.all.find { |r| r.tag == tag && (group.nil? || r.group == group) }
      end
      alias find_by_tag find_by_tag_and_group

      def file_path
        dir = File.dirname(__FILE__)
        File.expand_path '../../vendor/choosealicense.com/_data/rules.yml', dir
      end

      def raw_rules
        YAML.safe_load File.read(Rule.file_path)
      end

      def groups
        Rule.raw_rules.keys
      end
    end
  end
end