Class: Jekyll::GitHubMetadata::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-github-metadata/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Value

Returns a new instance of Value.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jekyll-github-metadata/value.rb', line 8

def initialize(*args)
  case args.size
  when 1
    @key = '{anonymous}'
    @value = args.first
  when 2
    @key = args.first.to_s
    @value = args.last
  else
    raise ArgumentError.new("#{args.size} args given but expected 1 or 2")
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/jekyll-github-metadata/value.rb', line 6

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/jekyll-github-metadata/value.rb', line 6

def value
  @value
end

Instance Method Details

#renderObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-github-metadata/value.rb', line 21

def render
  @value = if @value.respond_to?(:call)
    case @value.arity
    when 0
      @value.call
    when 1
      @value.call(GitHubMetadata.client)
    when 2
      @value.call(GitHubMetadata.client, GitHubMetadata.repository)
    else
      raise ArgumentError.new("Whoa, arity of 0, 1, or 2 please in your procs.")
    end
  else
    @value
  end
  @value = Sanitizer.sanitize(@value)
rescue RuntimeError, NameError => e
  Jekyll::GitHubMetadata.log :error, "Error processing value '#{key}':"
  raise e
end

#to_jsonObject



46
47
48
# File 'lib/jekyll-github-metadata/value.rb', line 46

def to_json(*)
  render.to_json
end

#to_liquidObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-github-metadata/value.rb', line 50

def to_liquid
  case render
  when nil
    nil
  when true, false
    value
  when Hash
    value
  when String, Numeric, Array
    value
  else
    to_json
  end
end

#to_sObject



42
43
44
# File 'lib/jekyll-github-metadata/value.rb', line 42

def to_s
  render.to_s
end