Class: TableSchema::Field
- Inherits:
-
Hash
- Object
- Hash
- TableSchema::Field
- Includes:
- Helpers
- Defined in:
- lib/tableschema/field.rb
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#missing_values ⇒ Object
readonly
Returns the value of attribute missing_values.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cast_type(value) ⇒ Object
- #cast_value(value, check_constraints: true) ⇒ Object
- #descriptor ⇒ Object
-
#initialize(descriptor, missing_values = nil) ⇒ Field
constructor
A new instance of Field.
- #test_value(value, check_constraints: true) ⇒ Object
Methods included from Helpers
#convert_to_boolean, #deep_symbolize_keys, #false_values, #get_class_for_type, #true_values, #type_class_lookup
Constructor Details
#initialize(descriptor, missing_values = nil) ⇒ Field
Returns a new instance of Field.
9 10 11 12 13 14 15 16 |
# File 'lib/tableschema/field.rb', line 9 def initialize(descriptor, missing_values=nil) self.merge! deep_symbolize_keys(descriptor) @name = self[:name] @type = self[:type] = self.fetch(:type, TableSchema::DEFAULTS[:type]) @format = self[:format] = self.fetch(:format, TableSchema::DEFAULTS[:format]) @constraints = self[:constraints] = self.fetch(:constraints, {}) @missing_values = missing_values || default_missing_values end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
7 8 9 |
# File 'lib/tableschema/field.rb', line 7 def constraints @constraints end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/tableschema/field.rb', line 7 def format @format end |
#missing_values ⇒ Object (readonly)
Returns the value of attribute missing_values.
7 8 9 |
# File 'lib/tableschema/field.rb', line 7 def missing_values @missing_values end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/tableschema/field.rb', line 7 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/tableschema/field.rb', line 7 def type @type end |
Instance Method Details
#cast_type(value) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/tableschema/field.rb', line 36 def cast_type(value) if is_null?(value) nil else type_class.new(self).cast(value) end end |
#cast_value(value, check_constraints: true) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/tableschema/field.rb', line 22 def cast_value(value, check_constraints: true) cast_value = cast_type(value) return cast_value if check_constraints == false TableSchema::Constraints.new(self, cast_value).validate! cast_value end |
#descriptor ⇒ Object
18 19 20 |
# File 'lib/tableschema/field.rb', line 18 def descriptor self.to_h end |
#test_value(value, check_constraints: true) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/tableschema/field.rb', line 29 def test_value(value, check_constraints: true) cast_value(value, check_constraints: check_constraints) true rescue TableSchema::Exception false end |