* added compile method
  * changed occurrences of "justask" to "Retrospring"
  * .generate now gets an option hash
  * documented some methods
This commit is contained in:
nilsding 2015-01-18 01:05:42 +01:00
parent 77c7f91241
commit d90f681be8
3 changed files with 62 additions and 13 deletions

View File

@ -1,6 +1,6 @@
# questiongenerator # questiongenerator
A simple question generator, used by justask. A simple question generator, used by Retrospring (formerly justask).
## Installation ## Installation
@ -10,7 +10,7 @@ Add this line to your application's Gemfile:
If you're feeling _edgy_, you can add this line instead: If you're feeling _edgy_, you can add this line instead:
gem 'questiongenerator', git: 'https://github.com/justask/questiongenerator.git' gem 'questiongenerator', git: 'https://github.com/retrospring/questiongenerator.git'
## Usage ## Usage
@ -21,18 +21,21 @@ require 'questiongenerator'
QuestionGenerator.question_base_path = '/home/nilsding/questions' QuestionGenerator.question_base_path = '/home/nilsding/questions'
QuestionGenerator.default_locale = :en QuestionGenerator.default_locale = :en
# Compile the questions for increased randomness
QuestionGenerator.compile
# Get some questions # Get some questions
puts QuestionGenerator.generate puts QuestionGenerator.generate
# => "What is the best thing about the internet?" # => "What is the best thing about the internet?"
# You can also specify the locale, if you want to # You can also specify the locale, if you want to
puts QuestionGenerator.generate :de puts QuestionGenerator.generate locale: :de
# => "Was war das letzte, das du gegessen hast?" # => "Was war das letzte, das du gegessen hast?"
``` ```
## Contributing ## Contributing
1. Fork it ( https://github.com/justask/questiongenerator/fork ) 1. Fork it ( https://github.com/retrospring/questiongenerator/fork )
2. Create your feature branch (`git checkout -b my-new-feature`) 2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`) 3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`) 4. Push to the branch (`git push origin my-new-feature`)

View File

@ -1,20 +1,49 @@
require 'yaml' require 'yaml'
# Generates some questions.
module QuestionGenerator module QuestionGenerator
VERSION = "0.0.2" # Version of QuestionGenerator
VERSION = "0.1.0"
class << self class << self
# The base path to the questions (e.g. +'/home/nilsding/questions'+).
attr_accessor :question_base_path attr_accessor :question_base_path
# The default locale, as a symbol.
attr_accessor :default_locale attr_accessor :default_locale
end end
def self.setup # Generates a new question.
yield self # @param options [Hash] A customizable set of options.
# @option options [Symbol] :locale (@default_locale) The target locale
# @option options [String] :prefix Prefix of the question, e.g. +'¿'+
# @option options [String] :suffix ('?') Suffix of the question, e.g. +' ?'+
# @option options [Boolean] :use_compiled (true) Use compiled questions
# instead of generating it. See also {compile}
# @return [String] String containing the generated question.
def self.generate(options = {})
opts = {
locale: @default_locale,
prefix: '',
suffix: '?',
use_compiled: true
}.merge!(options)
if opts[:use_compiled] and !@compiled[opts[:locale]].nil?
opts[:prefix] + @compiled[opts[:locale]].sample + opts[:suffix]
else
questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
opts[:prefix] + get_question(questions).strip + opts[:suffix]
end
end end
def self.generate(locale = @default_locale) # Compiles all the questions and stores it into the +@compiled+ hash.
questions = YAML.load_file(File.expand_path("#{locale.to_s}.yml", @question_base_path)) # @param options [Hash] A customizable set of options.
"#{get_question(questions).strip}?" # @option options [Symbol] :locale (@default_locale) The target locale
def self.compile(options = {})
opts = {
locale: @default_locale
}.merge!(options)
questions = YAML.load_file(File.expand_path("#{opts[:locale].to_s}.yml", @question_base_path))
@compiled[@default_locale] = build(questions)
end end
private private
@ -32,7 +61,24 @@ module QuestionGenerator
end end
question question
end end
def self.build(questions, q = "")
ary = []
if questions.is_a? Hash
questions.each do |k, v|
ary << build(v, "#{q}#{k} ")
end
elsif questions.is_a? Array
questions.each do |v|
ary << build(v, q)
end
elsif questions.is_a? String
return "#{q}#{questions}".strip
end
ary.flatten
end
@question_base_path = File.expand_path("../questions/", __FILE__) @question_base_path = File.expand_path("../questions/", __FILE__)
@default_locale = :en @default_locale = :en
@compiled = {}
end end

View File

@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
spec.authors = ["nilsding"] spec.authors = ["nilsding"]
spec.email = ["nilsding@nilsding.org"] spec.email = ["nilsding@nilsding.org"]
spec.summary = %q{A simple question generator.} spec.summary = %q{A simple question generator.}
spec.description = %q{A simple question generator.} spec.description = %q{A simple question generator, used by Retrospring.}
spec.homepage = "https://github.com/justask/questiongenerator" spec.homepage = "https://github.com/retrospring/questiongenerator"
spec.license = "MIT" spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0") spec.files = `git ls-files -z`.split("\x0")