Class: Typedocs::TypeSpec::HashValue

Inherits:
Typedocs::TypeSpec show all
Defined in:
lib/typedocs/type_spec.rb

Instance Method Summary collapse

Methods inherited from Typedocs::TypeSpec

#error_message_for

Constructor Details

#initialize(entries, accept_others) ⇒ HashValue

[key, spec]… ->



133
134
135
136
137
138
139
# File 'lib/typedocs/type_spec.rb', line 133

def initialize(entries, accept_others)
  entries.each do|k, s|
    Typedocs.ensure_klass(s, Typedocs::TypeSpec)
  end
  @entries = entries
  @accept_others = accept_others
end

Instance Method Details

#to_sourceObject



145
146
147
# File 'lib/typedocs/type_spec.rb', line 145

def to_source
  "{#{@entries.map{|key,value| "#{key.inspect} => #{value.to_source}"}.join(', ')}#{@accept_others ? ', ...' : ''}}"
end

#valid?(obj) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/typedocs/type_spec.rb', line 140

def valid?(obj)
  obj.is_a?(::Hash) &&
  (@accept_others || @entries.size == obj.size) &&
  @entries.all? {|key, spec| obj.has_key?(key) && spec.valid?(obj[key]) }
end