Class: JsonRecord::FieldDefinition
- Inherits:
-
Object
- Object
- JsonRecord::FieldDefinition
- Defined in:
- lib/json_record/field_definition.rb
Overview
A definition of a JSON field in a Schema.
Constant Summary collapse
- BOOLEAN_MAPPING =
{ true => true, 'true' => true, 'TRUE' => true, 'True' => true, 't' => true, 'T' => true, '1' => true, 1 => true, 1.0 => true, false => false, 'false' => false, 'FALSE' => false, 'False' => false, 'f' => false, 'F' => false, '0' => false, 0 => false, 0.0 => false, nil => false }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#convert(val) ⇒ Object
Convert a value to the proper class for storing it in the field.
-
#default ⇒ Object
Get the default value.
-
#initialize(name, options = {}) ⇒ FieldDefinition
constructor
Define a field.
-
#multivalued? ⇒ Boolean
Indicates the field is multivalued.
Constructor Details
#initialize(name, options = {}) ⇒ FieldDefinition
Define a field. Options should include :type with the class of the field. Other options available are :multivalued and :default.
13 14 15 16 17 18 19 20 21 |
# File 'lib/json_record/field_definition.rb', line 13 def initialize (name, = {}) @name = name.to_s @type = [:type] || String @multivalued = !![:multivalued] @default = [:default] if [Hash, Array].include?(@type) and @default.nil? @default = @type.new end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/json_record/field_definition.rb', line 9 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/json_record/field_definition.rb', line 9 def type @type end |
Instance Method Details
#convert(val) ⇒ Object
Convert a value to the proper class for storing it in the field. If the value can’t be converted, the original value will be returned. Blank values are always translated to nil. Hashes will be converted to EmbeddedDocument objects if the field type extends from EmbeddedDocument.
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 |
# File 'lib/json_record/field_definition.rb', line 42 def convert (val) return nil if val.blank? and val != false if @type == String return val.to_s elsif @type == Integer return Kernel.Integer(val) rescue val elsif @type == Float return Kernel.Float(val) rescue val elsif @type == Boolean v = BOOLEAN_MAPPING[val] v = val.to_s.downcase == 'true' if v.nil? # Check all mixed case spellings for true return v elsif @type == Date if val.is_a?(Date) return val elsif val.is_a?(Time) return val.to_date else return Date.parse(val.to_s) rescue val end elsif @type == Time if val.is_a?(Time) return Time.at((val.to_i / 60) * 60).utc else return Time.parse(val).utc rescue val end elsif @type == DateTime if val.is_a?(DateTime) return val.utc else return DateTime.parse(val).utc rescue val end elsif @type == Array val = [val] unless val.is_a?(Array) raise ArgumentError.new("#{name} must be an Array") unless val.is_a?(Array) return val elsif @type == Hash raise ArgumentError.new("#{name} must be a Hash") unless val.is_a?(Hash) return val elsif @type == BigDecimal return BigDecimal.new(val.to_s) else if val.is_a?(@type) val elsif val.is_a?(Hash) and (@type < EmbeddedDocument) return @type.new(val) else raise ArgumentError.new("#{name} must be a #{@type}") end end end |
#default ⇒ Object
Get the default value.
24 25 26 27 28 29 30 31 32 |
# File 'lib/json_record/field_definition.rb', line 24 def default if @default.nil? nil elsif @default.is_a?(Numeric) || @default.is_a?(Symbol) || @default.is_a?(TrueClass) || @default.is_a?(FalseClass) @default else @default.dup rescue @default end end |
#multivalued? ⇒ Boolean
Indicates the field is multivalued.
35 36 37 |
# File 'lib/json_record/field_definition.rb', line 35 def multivalued? @multivalued end |