Class: GithubStats::User

Inherits:
Object
  • Object
show all
Defined in:
lib/githubstats.rb

Overview

User object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ User

Creates a new user object


38
39
40
41
42
43
# File 'lib/githubstats.rb', line 38

def initialize(params = {})
  params = { name: params } unless params.is_a? Hash
  @name = params[:name] || guess_user
  @url = (params[:url] || DEFAULT_URL) % @name
  @last_updated = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)


101
102
103
104
105
106
# File 'lib/githubstats.rb', line 101

def method_missing(sym, *args, &block)
  load_data if @last_updated.nil?
  return super unless @data.respond_to? sym
  define_singleton_method(sym) { |*a, &b| @data.send(sym, *a, &b) }
  send(sym, *args, &block)
end

Instance Attribute Details

#data(reload = false) ⇒ Object (readonly)

Lazy loader for data


56
57
58
# File 'lib/githubstats.rb', line 56

def data
  @data
end

#last_updatedObject (readonly)

Returns the value of attribute last_updated


33
34
35
# File 'lib/githubstats.rb', line 33

def last_updated
  @last_updated
end

#nameObject (readonly)

Returns the value of attribute name


33
34
35
# File 'lib/githubstats.rb', line 33

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url


33
34
35
# File 'lib/githubstats.rb', line 33

def url
  @url
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Adjust respond_to? to properly respond with patched method_missing

Returns:

  • (Boolean)

64
65
66
67
# File 'lib/githubstats.rb', line 64

def respond_to?(method, include_private = false)
  load_data if @last_updated.nil?
  super || @data.respond_to?(method, include_private)
end

#to_sObject Also known as: inspect

Print human-readable string about object


48
49
50
# File 'lib/githubstats.rb', line 48

def to_s
  "Contributions from #{@name}"
end