class MetaModel::Record::Property

Attributes

modifiers[R]
name[RW]
type[R]

Public Class Methods

new(json_key, type = :string, *modifiers) click to toggle source
# 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
primary_id() click to toggle source
# File lib/metamodel/record/property.rb, line 22
def primary_id
  property = Property.new(:privateId, :int, :primary)
  property.name = :privateId
  property
end

Public Instance Methods

convert_symbol_to_type(symbol) click to toggle source
# 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_type() click to toggle source
# 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_value() click to toggle source
# File lib/metamodel/record/property.rb, line 84
def default_value
  has_default_value? ? modifiers[:default] : ""
end
has_default_value?() click to toggle source
# File lib/metamodel/record/property.rb, line 80
def has_default_value?
  !!@modifiers[:default]
end
is_array?() click to toggle source
# File lib/metamodel/record/property.rb, line 64
def is_array?
  @type.pluralize == str
end
is_optional?() click to toggle source
# File lib/metamodel/record/property.rb, line 76
def is_optional?
  @type.to_s.end_with? "?"
end
is_primary?() click to toggle source
# File lib/metamodel/record/property.rb, line 72
def is_primary?
  @modifiers.include? :primary
end
is_unique?() click to toggle source
# File lib/metamodel/record/property.rb, line 68
def is_unique?
  @modifiers.include? :unique
end
real_type() click to toggle source
# 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_optional() click to toggle source
# 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