Module: Vedeu::Repositories::Defaults Private

Includes:
Common
Included in:
Buffers::Clear, Buffers::Empty, Buffers::View, Cells::Empty, Colours::Colour, Editor::Document, Geometries::Move, Views::DefaultAttributes
Defined in:
lib/vedeu/repositories/defaults.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Some classes expect a certain set of attributes when initialized, this module uses the #defaults method of the class to provide missing attribute keys (and values).

Instance Method Summary collapse

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Instance Method Details

#defaultsHash<Symbol => void> (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The default options/attributes for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)


36
37
38
# File 'lib/vedeu/repositories/defaults.rb', line 36

def defaults
  {}
end

#initialize(attributes = {}) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

If a particular key is missing from the attributes parameter, then it is added with the respective value from #defaults.

Returns a new instance of the class including this module.

Parameters:

  • attributes (Hash) (defaults to: {})


27
28
29
30
31
# File 'lib/vedeu/repositories/defaults.rb', line 27

def initialize(attributes = {})
  defaults.merge!(validate(attributes)).each do |key, value|
    instance_variable_set("@#{key}", value || defaults.fetch(key))
  end
end

#validate(attributes) ⇒ Hash (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • attributes (Hash)

Returns:

  • (Hash)

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



43
44
45
46
47
48
# File 'lib/vedeu/repositories/defaults.rb', line 43

def validate(attributes)
  raise Vedeu::Error::InvalidSyntax,
        'Argument :attributes is not a Hash.' unless hash?(attributes)

  attributes.keep_if { |key, _| defaults.key?(key) }
end