Class: ActiveDoc::Descriptions::MethodArgumentDescription::OptionsHashArgumentExpectation

Inherits:
ArgumentExpectation
  • Object
show all
Defined in:
lib/active_doc/descriptions/method_argument_description.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ArgumentExpectation

find, #fulfilled?, inherited

Constructor Details

#initialize(argument) ⇒ OptionsHashArgumentExpectation

Returns a new instance of OptionsHashArgumentExpectation.



234
235
236
237
238
239
240
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 234

def initialize(argument)
  @proc = argument

  @hash_descriptions = ActiveDoc.nested_descriptions do
    Class.new.extend(Dsl).class_exec(&@proc)
  end
end

Class Method Details

.from(argument, options, proc) ⇒ Object



281
282
283
284
285
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 281

def self.from(argument, options, proc)
  if proc.is_a?(Proc) && proc.arity == 0 && argument == Hash
    self.new(proc)
  end
end

Instance Method Details

#additional_rdocObject



269
270
271
272
273
274
275
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 269

def additional_rdoc
  if @hash_descriptions
    ret = @hash_descriptions.map { |x| "  #{x.to_rdoc(true)}" }.join("\n")
    ret.insert(0, ":\n")
    ret
  end
end

#condition?(value, args_with_vals) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 242

def condition?(value, args_with_vals)
  if @hash_descriptions
    raise "Only hash is supported for nested argument documentation" unless value.is_a? Hash
    hash_args_with_vals = value.inject(Hash.new{|h,k| h[k] = {:defined => false}}) do |hash, (key,val)|
      hash[key] = {:val => val, :defined => true}
      hash
    end
    described_keys   = @hash_descriptions.map do |hash_description|
      hash_description.validate(hash_args_with_vals)
    end
    undescribed_keys = value.keys - described_keys
    unless undescribed_keys.empty?
      raise ArgumentError.new("Inconsistent options definition with active doc. Hash was not expected to have arguments '#{undescribed_keys.join(", ")}'")
    end
  end
  return true
end

#expectation_fail_to_sObject

Expected to…



261
262
263
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 261

def expectation_fail_to_s
  "contain described keys, got #{@failed_value.inspect}"
end

#last_lineObject



277
278
279
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 277

def last_line
  @hash_descriptions && @hash_descriptions.last && (@hash_descriptions.last.last_line + 1)
end

#to_rdocObject



265
266
267
# File 'lib/active_doc/descriptions/method_argument_description.rb', line 265

def to_rdoc
  return "Hash"
end