Class: MSFL::Datasets::Base
- Inherits:
-
Object
- Object
- MSFL::Datasets::Base
- Includes:
- Validators::Definitions::HashKey
- Defined in:
- lib/msfl/datasets/base.rb
Class Method Summary collapse
-
.register_dataset(dataset = nil, opts = {}) ⇒ Object
Register a MSFL::Dataset as a registered dataset so that other code can reference the dataset using its name as a symbol, instead of having to pass around the class name.
- .registered_datasets ⇒ Object
- .registered_datasets=(registered_datasets) ⇒ Object
Instance Method Summary collapse
-
#fields ⇒ Array<Symbol>
The descendant class MUST override this method otherwise all field validations will fail.
-
#operator_conforms?(operator, field) ⇒ Bool
Method not implemented at this time.
-
#operators ⇒ Array<Symbol>
The descendant class may override this method to control the operators that are supported for the dataset - Note that this can only be used to reduce the number of supported operators (you can’t add new operators here, without first adding them to MSFL::Validators::Definitions::HashKey#hash_key_operators).
-
#type_conforms?(obj, field) ⇒ Boolean
Method not implemented at this time Returns true if the object conforms to the types supported by the indicated field While not currently implemented the intent is that the descendant Dataset would specify a hash of supported types for each field and this method would then cross reference that list.
-
#validate_operator_conforms(operator, field, errors) ⇒ Array
This method returns the errors argument.
-
#validate_type_conforms(obj, field, errors) ⇒ Array
This method returns the errors argument.
-
#validate_value_conforms(value, field, errors) ⇒ Array
This method returns the errors argument.
-
#value_conforms?(value, field, errors = []) ⇒ Bool
Method not implemented at this time.
Methods included from Validators::Definitions::HashKey
#all_logical_operators?, #all_operators?, #any_operators?, #binary_operators, #hash_key_operators, #logical_operators, #valid_hash_key?, #valid_hash_keys
Class Method Details
.register_dataset(dataset = nil, opts = {}) ⇒ Object
add tests
Register a MSFL::Dataset as a registered dataset so that other code can reference the dataset using its name as a symbol, instead of having to pass around the class name.
If no arguments are provided it registers the current class and sets its name to the class name downcased The dataset being registered can be overridden. The dataset name (how one refers to the dataset as a symbol)
can also be overridden.
@meta-spinach
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/msfl/datasets/base.rb', line 33 def self.register_dataset(dataset = nil, opts = {}) dataset ||= self dataset_name = opts[:name] if opts.has_key?(:name) dataset_name ||= dataset.name dataset_name.slice! "MSFL::Datasets::" dataset_name.downcase! registered_datasets = MSFL::Datasets::Base.registered_datasets registered_datasets[dataset_name.to_sym] = dataset MSFL::Datasets::Base.registered_datasets = registered_datasets end |
.registered_datasets ⇒ Object
10 11 12 |
# File 'lib/msfl/datasets/base.rb', line 10 def registered_datasets @registered_datasets ||= {} end |
.registered_datasets=(registered_datasets) ⇒ Object
14 15 16 |
# File 'lib/msfl/datasets/base.rb', line 14 def registered_datasets=(registered_datasets) @registered_datasets = registered_datasets end |
Instance Method Details
#fields ⇒ Array<Symbol>
The descendant class MUST override this method otherwise all field validations will fail
The method defines an array of symbols, indicating what fields are supported for the Dataset
49 50 51 |
# File 'lib/msfl/datasets/base.rb', line 49 def fields raise NoMethodError, "Descendants of MSFL::Datasets::Base are required to implement the #fields method" end |
#operator_conforms?(operator, field) ⇒ Bool
Method not implemented at this time
This method returns true if the operator is supported for the specified field by the dataset. While this is not currently implemented, the intent is that a hash of fields (as keys) would map to Arrays<Symbol> (as values) and then this method would validate that the operator argument meets this contract.
111 112 113 |
# File 'lib/msfl/datasets/base.rb', line 111 def operator_conforms?(operator, field) true end |
#operators ⇒ Array<Symbol>
The descendant class may override this method to control the operators that are supported for the dataset
- Note that this can only be used to reduce the number of supported operators (you can't add new operators
here, without first adding them to MSFL::Validators::Definitions::HashKey#hash_key_operators)
58 59 60 |
# File 'lib/msfl/datasets/base.rb', line 58 def operators hash_key_operators end |
#type_conforms?(obj, field) ⇒ Boolean
Method not implemented at this time Returns true if the object conforms to the types supported by the indicated field While not currently implemented the intent is that the descendant Dataset would specify a hash of supported types for each field and this method would then cross reference that list.
86 87 88 |
# File 'lib/msfl/datasets/base.rb', line 86 def type_conforms?(obj, field) true end |
#validate_operator_conforms(operator, field, errors) ⇒ Array
This method returns the errors argument. The errors argument is unchanged if operator conformity validation passes, otherwise an error is added to errors.
97 98 99 100 |
# File 'lib/msfl/datasets/base.rb', line 97 def validate_operator_conforms(operator, field, errors) errors << "Dataset operator conformity validation failed for operator: #{operator} against field: #{field}" unless operator_conforms?(operator, field) errors end |
#validate_type_conforms(obj, field, errors) ⇒ Array
This method returns the errors argument. The errors argument is unchanged if type conformity validation passes, otherwise an error is added to errors.
@example:
foo.investors_validate_type_conforms("abc", :total_funding) => # there is one more error in errors
# because the type of total_funding must be an integer
74 75 76 77 |
# File 'lib/msfl/datasets/base.rb', line 74 def validate_type_conforms(obj, field, errors) errors << "Dataset type conformity validation failed for obj: #{obj} against field: #{field}" unless type_conforms?(obj, field) errors end |
#validate_value_conforms(value, field, errors) ⇒ Array
This method returns the errors argument. The errors argument is unchanged if value conformity validation passes, otherwise an error is added to errors.
@example:
foo.investors_value_conforms(-6000, :total_funding) => # there is one more error in errors
# because the funding cannot be negative
@example:
foo.investors_value_conforms(12345, :total_funding) => # errors is unchanged
133 134 135 136 |
# File 'lib/msfl/datasets/base.rb', line 133 def validate_value_conforms(value, field, errors) errors << "Dataset value conformity validation failed for value: #{value} against field: #{field}" unless value_conforms?(value, field, errors) errors end |
#value_conforms?(value, field, errors = []) ⇒ Bool
Method not implemented at this time
This method returns true if the value is supported for the specified field by the dataset. While this is not currently implemented, the intent is that a hash of fields (as keys) would map to an Array of validation constraints. These constraints would then be executed against the value and if all are successful the value would be considered to have passed.
It is likely that the methods invoked from the Array of validation constraints would actually return an Array of errors encountered, this method would then concat that Array into the errors array. If the encountered errors array is empty the method would return true, and false otherwise.
153 154 155 |
# File 'lib/msfl/datasets/base.rb', line 153 def value_conforms?(value, field, errors = []) true end |