Module: Remarkable::DSL::Optionals

Defined in:
lib/remarkable/dsl/optionals.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OPTIONAL_KEYS =
[ :positive, :negative, :not_given ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/remarkable/dsl/optionals.rb', line 7

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#description(options = {}) ⇒ Object

Overwrites description to support optionals. Check optional for more information.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/remarkable/dsl/optionals.rb', line 114

def description(options={})
  message = super(options)
  message.strip!

  optionals = self.class.matcher_optionals.map do |optional|
    scope = matcher_i18n_scope + ".optionals.#{optional}"

    if @options.key?(optional)
      value = @options[optional]
      defaults = [ (value ? :positive : :negative) ]

      # If optional is a symbol and it's not any to any of the reserved symbols, search for it also
      defaults.unshift(value) if value.is_a?(Symbol) && !OPTIONAL_KEYS.include?(value)

      Remarkable.t defaults.shift, :default => defaults, :raise => true, :scope => scope,
                                   :inspect => value.inspect, :value => value.to_s
    else
      Remarkable.t :not_given, :raise => true, :scope => scope
    end rescue nil
  end.compact

  message << ' ' << array_to_sentence(optionals)
  message.strip!
  message
end