Class: HumanPower::Generator

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

Constant Summary collapse

DISALLOW_KEYS =
{ all: "/", none: "" }

Instance Method Summary collapse

Constructor Details

#initialize(context = nil, &block) ⇒ Generator

Returns a new instance of Generator.



5
6
7
8
9
# File 'lib/human_power/generator.rb', line 5

def initialize(context = nil, &block)
  @context = context
  @current_agent_string = "*"
  instance_eval &block if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/human_power/generator.rb', line 86

def method_missing(method, *args, &block)
  if block && HumanPower.user_agents.has_key?(method)
    user_agent method, &block
  elsif context
    context.send method, *args, &block
  else
    super
  end
end

Instance Method Details

#add_rule(type, *paths) ⇒ Object



27
28
29
# File 'lib/human_power/generator.rb', line 27

def add_rule(type, *paths)
  rules[@current_agent_string].push *paths.map { |path| path.is_a?(Symbol) ? DISALLOW_KEYS.fetch(path) : path.to_s }.map { |path| HumanPower::Rule.new(type, path) }
end

#add_tree_rule(type, *paths) ⇒ Object



31
32
33
# File 'lib/human_power/generator.rb', line 31

def add_tree_rule(type, *paths)
  add_rule type, *paths.map { |path| path.end_with?("/") ? path : "#{path}/" }
end

#allow(*paths) ⇒ Object



35
36
37
# File 'lib/human_power/generator.rb', line 35

def allow(*paths)
  add_rule :allow, *paths
end

#allow_tree(*paths) ⇒ Object



39
40
41
# File 'lib/human_power/generator.rb', line 39

def allow_tree(*paths)
  add_tree_rule :allow, *paths
end

#contextObject



63
64
65
# File 'lib/human_power/generator.rb', line 63

def context
  @context
end

#disallow(*paths) ⇒ Object



43
44
45
# File 'lib/human_power/generator.rb', line 43

def disallow(*paths)
  add_rule :disallow, *paths
end

#disallow_tree(*paths) ⇒ Object



47
48
49
# File 'lib/human_power/generator.rb', line 47

def disallow_tree(*paths)
  add_tree_rule :disallow, *paths
end

#renderObject Also known as: to_s



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/human_power/generator.rb', line 67

def render
  sections = []

  if sitemaps.any?
    sections << sitemaps.uniq.map { |url| "Sitemap: #{url}" }.join("\n")
  end
  
  agents = rules.keys.sort.map do |agent|
    "User-agent: #{agent}\n" +
    rules[agent].uniq.map(&:render).join("\n")
  end
  
  sections.push *agents

  sections.join("\n\n")
end

#rulesObject



55
56
57
# File 'lib/human_power/generator.rb', line 55

def rules
  @rules ||= Hash.new { |h, k| h[k] = [] }
end

#sitemap(*sitemap_urls) ⇒ Object



51
52
53
# File 'lib/human_power/generator.rb', line 51

def sitemap(*sitemap_urls)
  sitemaps.push *sitemap_urls
end

#sitemapsObject



59
60
61
# File 'lib/human_power/generator.rb', line 59

def sitemaps
  @sitemaps ||= []
end

#user_agent(key_or_string, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/human_power/generator.rb', line 11

def user_agent(key_or_string, &block)
  old_agent_string = @current_agent_string
  
  [key_or_string].flatten.each do |agent|      
    @current_agent_string = case agent
    when String then agent
    when Symbol then HumanPower.user_agents.fetch(agent)
    else raise ArgumentError, "Please supply a string or symbol to `user_agent`."
    end

    instance_eval &block
  end

  @current_agent_string = old_agent_string
end