0.1.0!
* added compile method * changed occurrences of "justask" to "Retrospring" * .generate now gets an option hash * documented some methods
This commit is contained in:
parent
77c7f91241
commit
d90f681be8
11
README.md
11
README.md
|
@ -1,6 +1,6 @@
|
|||
# questiongenerator
|
||||
|
||||
A simple question generator, used by justask.
|
||||
A simple question generator, used by Retrospring (formerly justask).
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -10,7 +10,7 @@ Add this line to your application's Gemfile:
|
|||
|
||||
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
|
||||
|
||||
|
@ -21,18 +21,21 @@ require 'questiongenerator'
|
|||
QuestionGenerator.question_base_path = '/home/nilsding/questions'
|
||||
QuestionGenerator.default_locale = :en
|
||||
|
||||
# Compile the questions for increased randomness
|
||||
QuestionGenerator.compile
|
||||
|
||||
# Get some questions
|
||||
puts QuestionGenerator.generate
|
||||
# => "What is the best thing about the internet?"
|
||||
|
||||
# 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?"
|
||||
```
|
||||
|
||||
## 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`)
|
||||
3. Commit your changes (`git commit -am 'Add some feature'`)
|
||||
4. Push to the branch (`git push origin my-new-feature`)
|
||||
|
|
|
@ -1,20 +1,49 @@
|
|||
require 'yaml'
|
||||
|
||||
# Generates some questions.
|
||||
module QuestionGenerator
|
||||
VERSION = "0.0.2"
|
||||
# Version of QuestionGenerator
|
||||
VERSION = "0.1.0"
|
||||
|
||||
class << self
|
||||
# The base path to the questions (e.g. +'/home/nilsding/questions'+).
|
||||
attr_accessor :question_base_path
|
||||
# The default locale, as a symbol.
|
||||
attr_accessor :default_locale
|
||||
end
|
||||
|
||||
def self.setup
|
||||
yield self
|
||||
# Generates a new question.
|
||||
# @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
|
||||
|
||||
def self.generate(locale = @default_locale)
|
||||
questions = YAML.load_file(File.expand_path("#{locale.to_s}.yml", @question_base_path))
|
||||
"#{get_question(questions).strip}?"
|
||||
|
||||
# Compiles all the questions and stores it into the +@compiled+ hash.
|
||||
# @param options [Hash] A customizable set of options.
|
||||
# @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
|
||||
|
||||
private
|
||||
|
@ -32,7 +61,24 @@ module QuestionGenerator
|
|||
end
|
||||
question
|
||||
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__)
|
||||
@default_locale = :en
|
||||
@compiled = {}
|
||||
end
|
||||
|
|
|
@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|||
spec.authors = ["nilsding"]
|
||||
spec.email = ["nilsding@nilsding.org"]
|
||||
spec.summary = %q{A simple question generator.}
|
||||
spec.description = %q{A simple question generator.}
|
||||
spec.homepage = "https://github.com/justask/questiongenerator"
|
||||
spec.description = %q{A simple question generator, used by Retrospring.}
|
||||
spec.homepage = "https://github.com/retrospring/questiongenerator"
|
||||
spec.license = "MIT"
|
||||
|
||||
spec.files = `git ls-files -z`.split("\x0")
|
||||
|
|
Loading…
Reference in New Issue