lib/unbreakable/scraper.rb in unbreakable-0.0.3 vs lib/unbreakable/scraper.rb in unbreakable-0.0.4
- old
+ new
@@ -1,10 +1,10 @@
require 'forwardable'
require 'optparse'
require 'securerandom'
-require 'active_support/inflector/methods'
+require 'active_support/core_ext/class/attribute_accessors'
module Unbreakable
# You may implement a scraper by subclassing this class:
#
# require 'open-uri'
@@ -42,10 +42,13 @@
extend Forwardable
def_delegators :@app, :add_child_configurable, :configure, :datastore,
:fetch, :log, :processor
+ cattr_accessor :commands
+ @@commands = []
+
# Initializes a Dragonfly app for storage and processing.
def initialize
@app = Dragonfly[SecureRandom.hex.to_sym]
# defaults to Logger.new('/var/tmp/dragonfly.log')
@app.log = Logger.new(STDOUT)
@@ -71,22 +74,42 @@
config Print the current configuration
eos
@opts.separator ''
@opts.separator 'Specific options:'
+ specific_options
extract_configuration @app
@opts.separator ''
@opts.separator 'General options:'
+ general_options
@opts.on_tail('-h', '--help', 'Display this screen') do
puts @opts
exit
end
end
@opts
end
+ # def specific_options
+ # @opts.on('--echo ARG', 'Write a string to standard output') do |x|
+ # puts x
+ # end
+ # end
+ #
+ # @abstract Override to add specific options to the option parser.
+ def specific_options; end
+
+ # def general_options
+ # @opts.on('--echo ARG', 'Write a string to standard output') do |x|
+ # puts x
+ # end
+ # end
+ #
+ # @abstract Override to add general options to the option parser.
+ def general_options; end
+
# Runs the command. Most often run from a command-line script as:
#
# scraper.run(ARGV)
#
# @param [Array] args command-line arguments
@@ -102,11 +125,16 @@
when 'config'
print_configuration @app
when nil
puts opts
else
- opts.abort "'#{command}' is not a #{opts.program_name} command. See '#{opts.program_name} --help'."
+ # Allow subclasses to add more commands.
+ if self.commands.include? command.to_sym
+ send command, args
+ else
+ opts.abort "'#{command}' is not a #{opts.program_name} command. See '#{opts.program_name} --help'."
+ end
end
end
# Stores a record in the datastore.
# @param [Hash] opts options to pass to the datastore
@@ -169,33 +197,35 @@
private
# @param [#configuration] object
def extract_configuration(object)
- object.default_configuration.merge(object.configuration).each do |key,value|
- if true === value or false === value
- @opts.on("--[no-]#{key}", "default #{value.inspect}") do |x|
- object.send "#{key}=", x
+ object.config_methods.each do |meth|
+ default = object.configuration[meth] || object.default_configuration[meth]
+ if true === default or false === default
+ @opts.on("--[no-]#{meth}", "default #{default.inspect}") do |x|
+ object.configure{|c| c.send "#{meth}=", x}
end
- elsif String === value or Fixnum === value
- @opts.on("--#{key} ARG", "default #{value.inspect}") do |x|
- object.send "#{key}=", x
+ elsif String === default or Fixnum === default
+ @opts.on("--#{meth} ARG", "default #{default.inspect}") do |x|
+ object.configure{|c| c.send "#{meth}=", x}
end
- elsif object != value and value.respond_to? :configuration
- extract_configuration value
+ elsif object != default and default.respond_to? :configuration
+ extract_configuration default
end
end
end
# @param [#configuration] object
def print_configuration(object, indent = 0)
indentation = ' ' * indent
puts "#{indentation}#{object.class.name}:"
- object.default_configuration.merge(object.configuration).each do |key,value|
- if true === value or false === value or String === value or Fixnum === value
- puts " #{indentation}#{key.to_s.ljust 25 - indent}#{value.inspect}"
- elsif object != value and value.respond_to? :configuration
- print_configuration value, indent + 2
+ object.config_methods.each do |meth|
+ default = object.configuration[meth] || object.default_configuration[meth]
+ if true === default or false === default or String === default or Fixnum === default
+ puts " #{indentation}#{meth.to_s.ljust 25 - indent}#{default.inspect}"
+ elsif object != default and default.respond_to? :configuration
+ print_configuration default, indent + 2
end
end
end
end
end