File: owner.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (90 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download | duplicates (3)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

module Jekyll
  module GitHubMetadata
    class Owner
      extend Forwardable

      # Defines an instance method that delegates to a hash's key
      #
      # hash   - a symbol representing the instance method to delegate to. The
      #          instance method should return a hash or respond to #[]
      # key    - the key to call within the hash
      # method - (optional) the instance method the key should be aliased to.
      #          If not specified, defaults to the hash key
      #
      # Returns a symbol representing the instance method
      def self.def_hash_delegators(hash, *methods)
        content_methods.concat(methods)
        methods.each do |method|
          define_method(method) do
            send(hash)[method.to_s]
          end
        end
      end

      def self.content_methods
        @content_methods ||= []
      end

      # List of whitelisted keys.
      def_hash_delegators :owner_info,
                          :avatar_url,
                          :bio,
                          :blog,
                          :collaborators,
                          :company,
                          :created_at,
                          :description,
                          :email,
                          :followers,
                          :following,
                          :has_organization_projects,
                          :has_repository_projects,
                          :hireable,
                          :html_url,
                          :id,
                          :is_verified,
                          :location,
                          :login,
                          :name,
                          :node_id,
                          :public_gists,
                          :public_gists,
                          :public_repos,
                          :public_repos,
                          :type,
                          :updated_at

      attr_reader :owner_login

      def initialize(owner_login)
        @owner_login = owner_login
      end

      def to_h
        @to_h ||= self.class.content_methods
          .each_with_object({}) { |method, hash| hash[method.to_s] = public_send(method) }
      end
      alias_method :to_hash, :to_h
      def_delegator :to_h, :to_json, :to_json
      def_delegator :to_h, :to_liquid, :to_liquid

      def_delegator :to_h, :to_s, :to_s
      alias_method :to_str, :to_s

      private

      def owner_info
        @owner_info ||= begin
          Value.new(
            "owner",
            proc do |c|
              (c.organization(owner_login) || c.user(owner_login) || {}).to_h
            end
          ).render || {}
        end
      end
    end
  end
end