From d90f681be8e3a155a6aa45fc9afaae95c24d5114 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sun, 18 Jan 2015 01:05:42 +0100 Subject: [PATCH] 0.1.0! * added compile method * changed occurrences of "justask" to "Retrospring" * .generate now gets an option hash * documented some methods --- README.md | 11 ++++--- lib/questiongenerator.rb | 60 ++++++++++++++++++++++++++++++++++----- questiongenerator.gemspec | 4 +-- 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1d706f6..d916df3 100644 --- a/README.md +++ b/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`) diff --git a/lib/questiongenerator.rb b/lib/questiongenerator.rb index 92c882e..0344bad 100644 --- a/lib/questiongenerator.rb +++ b/lib/questiongenerator.rb @@ -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 diff --git a/questiongenerator.gemspec b/questiongenerator.gemspec index 4c016c0..ea3100a 100644 --- a/questiongenerator.gemspec +++ b/questiongenerator.gemspec @@ -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")