Class: Rosette::Core::Commands::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/core/commands.rb

Overview

Base class for all Rosette commands.

Direct Known Subclasses

GitCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Command

Creates a new command instance.

Parameters:

  • configuration (Configurator)

    The Rosette configuration to use.


82
83
84
# File 'lib/rosette/core/commands.rb', line 82

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationConfigurator (readonly)

Returns Rosette configuration.

Returns:


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rosette/core/commands.rb', line 39

class Command
  attr_reader :configuration

  class << self    # Validates a single field.
    #
    # @param [Symbol] field The field to validate.
    # @param [Hash] validator_hash The hash of options for this
    #   validation. For now, should just contain +:type+ which contains
    #   to a symbol corresponding to the type of validator to use.
    # @return [void]

    def validate(field, validator_hash)
      validators[field] << instantiate_validator(validator_hash)
    end

    # A hash of all the currently configured validators.
    #
    # @return [Hash<Symbol, Array<Validator>>] The hash of fields to
    #   valdiator instances.
    def validators
      @validators ||= Hash.new { |hash, key| hash[key] = [] }
    end

    private

    def instantiate_validator(validator_hash)
      validator_type = validator_hash.fetch(:type)
      validator_class_for(validator_type).new(validator_hash)
    end

    def validator_class_for(validator_type)
      klass = "#{Rosette::Core::StringUtils.camelize(validator_type.to_s)}Validator"
      if Rosette::Core::Validators.const_defined?(klass)
        Rosette::Core::Validators.const_get(klass)
      else
        raise TypeError, "couldn't find #{validator_type} validator"
      end
    end
  end

  # Creates a new command instance.
  #
  # @param [Configurator] configuration The Rosette configuration to use.
  def initialize(configuration)
    @configuration = configuration
  end

  # Gets the hash of current error messages.
  #
  # @return [Hash<Symbol, Array<String>>] The hash of current messages.
  #   The hash's keys are field names and the values are arrays of error
  #   messages for that field.
  def messages
    @messages ||= {}
  end

  # Returns true if the command's validators all pass, false otherwise.
  #
  # @raise [NotImplementedError]
  def valid?
    raise NotImplementedError, 'please use a Command subclass.'
  end

  protected

  def datastore
    configuration.datastore
  end

  def path_digest(paths)
    Digest::MD5.hexdigest(paths.join)
  end
end

Class Method Details

.validate(field, validator_hash) ⇒ void

This method returns an undefined value.

Validates a single field.

Parameters:

  • field (Symbol)

    The field to validate.

  • validator_hash (Hash)

    The hash of options for this validation. For now, should just contain :type which contains to a symbol corresponding to the type of validator to use.


50
51
52
# File 'lib/rosette/core/commands.rb', line 50

def validate(field, validator_hash)
  validators[field] << instantiate_validator(validator_hash)
end

.validatorsHash<Symbol, Array<Validator>>

A hash of all the currently configured validators.

Returns:

  • (Hash<Symbol, Array<Validator>>)

    The hash of fields to valdiator instances.


58
59
60
# File 'lib/rosette/core/commands.rb', line 58

def validators
  @validators ||= Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#messagesHash<Symbol, Array<String>>

Gets the hash of current error messages.

Returns:

  • (Hash<Symbol, Array<String>>)

    The hash of current messages. The hash's keys are field names and the values are arrays of error messages for that field.


91
92
93
# File 'lib/rosette/core/commands.rb', line 91

def messages
  @messages ||= {}
end

#valid?Boolean

Returns true if the command's validators all pass, false otherwise.

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

98
99
100
# File 'lib/rosette/core/commands.rb', line 98

def valid?
  raise NotImplementedError, 'please use a Command subclass.'
end