Class: MetaModel::Record::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/metamodel/record/property.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_key, type = :string, *modifiers) ⇒ Property

Returns a new instance of Property.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/metamodel/record/property.rb', line 8

def initialize(json_key, type = :string, *modifiers)
  @name = json_key.to_s.camelize(:lower)
  @type = convert_symbol_to_type type

  @modifiers = {}
  @modifiers.default = false

  modifiers.flatten.map do |modifier|
    @modifiers[modifier] = true if modifier.is_a? Symbol
    @modifiers[:default] = modifier[:default] if modifier.is_a? Hash and modifier[:default]
  end
end

Instance Attribute Details

#modifiersObject (readonly)

Returns the value of attribute modifiers.



6
7
8
# File 'lib/metamodel/record/property.rb', line 6

def modifiers
  @modifiers
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/metamodel/record/property.rb', line 4

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/metamodel/record/property.rb', line 5

def type
  @type
end

Class Method Details

.primary_idObject



22
23
24
25
26
# File 'lib/metamodel/record/property.rb', line 22

def primary_id
  property = Property.new(:privateId, :int, :primary)
  property.name = :privateId
  property
end

Instance Method Details

#convert_symbol_to_type(symbol) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/metamodel/record/property.rb', line 52

def convert_symbol_to_type(symbol)
  case symbol
  when :int    then "Int"
  when :double then "Double"
  when :bool   then "Bool"
  when :string then "String"
  when :date   then "Date"
  else symbol.to_s.camelize
  end
end

#database_typeObject



34
35
36
37
38
39
40
41
# File 'lib/metamodel/record/property.rb', line 34

def database_type
  case type_without_optional
    when "String" then "TEXT"
    when "Int", "Bool" then "INTEGER"
    when "Double", "Date", "Float" then "REAL"
    else raise Informative, "Unsupported type #{self.type}"
  end
end

#default_valueObject



84
85
86
# File 'lib/metamodel/record/property.rb', line 84

def default_value
  has_default_value? ? modifiers[:default] : ""
end

#has_default_value?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/metamodel/record/property.rb', line 80

def has_default_value?
  !!@modifiers[:default]
end

#is_array?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/metamodel/record/property.rb', line 64

def is_array?
  @type.pluralize == str
end

#is_optional?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/metamodel/record/property.rb', line 76

def is_optional?
  @type.to_s.end_with? "?"
end

#is_primary?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/metamodel/record/property.rb', line 72

def is_primary?
  @modifiers.include? :primary
end

#is_unique?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/metamodel/record/property.rb', line 68

def is_unique?
  @modifiers.include? :unique
end

#real_typeObject



43
44
45
46
47
48
49
50
# File 'lib/metamodel/record/property.rb', line 43

def real_type
  case type_without_optional
  when "String" then "String"
  when "Int", "Bool" then "Int64"
  when "Double", "Date", "Float" then "Double"
  else raise Informative, "Unsupported type #{self.type}"
  end
end

#type_without_optionalObject



29
30
31
32
# File 'lib/metamodel/record/property.rb', line 29

def type_without_optional
  return type.to_s[0..-2] if type.to_s.end_with? "?"
  type
end