Class: JSONdb::Field

Inherits:
Object
  • Object
show all
Includes:
Logger, Validations::Naming, Validations::Types
Defined in:
lib/jsondb/field.rb

Constant Summary

Constants included from Validations::Types

Validations::Types::AllowedTypes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#allowed_log_level?, #log, #log_enabled?, #log_this?

Methods included from Validations::Types

#allowed_type?, #validate_type

Methods included from Validations::Naming

#allowed_name?

Constructor Details

#initialize(name) ⇒ Field

Returns a new instance of Field.



11
12
13
14
15
16
17
18
19
20
# File 'lib/jsondb/field.rb', line 11

def initialize(name)
  @name = name if allowed_name?(name)

  # override common setters
  self.type       = "String"
  self.nullable   = true
  self.default    = nil

  # set_defaults
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/jsondb/field.rb', line 9

def name
  @name
end

Instance Method Details

#defaultObject



60
61
62
# File 'lib/jsondb/field.rb', line 60

def default
  @default
end

#default=(value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/jsondb/field.rb', line 42

def default=(value)
  if validate_type(@type, value)
    @default = value
    return true
  else
    log("'#{value}' not allowed as #{type} class.")
    return false
  end
end

#nullableObject



52
53
54
# File 'lib/jsondb/field.rb', line 52

def nullable
  @nullable
end

#nullable=(value) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/jsondb/field.rb', line 32

def nullable=(value)
  if validate_type("Bool", value)
    @nullable = value
    return true
  else
    log("'#{value}' not allowed for nullable", :error)
    return false
  end
end

#to_hashObject



64
65
66
67
68
69
70
# File 'lib/jsondb/field.rb', line 64

def to_hash
  {
    "type"      => @type,
    "nullable"  => @nullable,
    "default"   => @default
  }
end

#typeObject



56
57
58
# File 'lib/jsondb/field.rb', line 56

def type
  @type
end

#type=(_class) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/jsondb/field.rb', line 22

def type=(_class)
  if allowed_type?(_class)
    @type = _class  
    return true
  else
    log("'#{_class}' not allowed as field type", :error)
    return false
  end
end