enju
Version:
An elasticsearch on node.js written in CoffeeScript.
272 lines (261 loc) • 10.3 kB
text/coffeescript
util = require 'util'
exceptions = require './exceptions'
class Property
###
default {bool}
required {bool}
dbField {string}
type {string} For elasticsearch mapping
index {string} For elasticsearch mapping
analyzer {string} For elasticsearch mapping
mapping {object} For elasticsearch mapping
propertyName {string} The property name in the document. It will be set at Document.define()
###
constructor: (args={}) ->
{
,
,
,
,
,
,
,
} = args
?= no
class StringProperty extends Property
constructor: (args) ->
super args
toJs: (value) ->
###
Convert value for initial Document.
classInstance: {Document} The instance of the document.
{string}
###
if not value?
if ?
return .toString()
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
value.toString()
toDb: (classInstance) ->
###
Convert value for writing database.
classInstance: {Document} The instance of the document.
{string}
###
value = classInstance[]
if not value?
if ?
classInstance[] = .toString()
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
value.toString()
exports.StringProperty = StringProperty
class IntegerProperty extends Property
constructor: (args) ->
super args
toJs: (value) ->
if not value?
if ?
return parseInt
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
parseInt value
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] = parseInt
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
parseInt value
exports.IntegerProperty = IntegerProperty
class FloatProperty extends Property
constructor: (args) ->
super args
toJs: (value) ->
if not value?
if ?
return parseFloat
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
parseFloat value
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] = parseFloat
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
parseFloat value
exports.FloatProperty = FloatProperty
class BooleanProperty extends Property
constructor: (args) ->
super args
toJs: (value) ->
if not value?
if ?
return Boolean
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
Boolean value
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] = Boolean
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
Boolean value
exports.BooleanProperty = BooleanProperty
class DateProperty extends Property
constructor: (args={}) ->
{} = args
super args
toJs: (value) ->
if not value?
if
return new Date()
else if ?
return new Date()
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
new Date(value)
toDb: (classInstance) ->
value = classInstance[]
if not value?
if
classInstance[] = new Date()
return classInstance[].toJSON()
else if ?
classInstance[] = new Date()
return classInstance[].toJSON()
else
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
value.toJSON()
exports.DateProperty = DateProperty
class ListProperty extends Property
constructor: (args={}) ->
{} = args
super args
toJs: (value) ->
if not value?
if ?
return Array.apply(@, )
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
if ?
switch
when StringProperty
value = ((if x? then x.toString() else null) for x in value)
when IntegerProperty
value = ((if x? then parseInt(x) else null) for x in value)
when FloatProperty
value = ((if x? then parseFloat(x) else null) for x in value)
when BooleanProperty
value = ((if x? then Boolean(x) else null) for x in value)
when DateProperty
value = ((if x? then x.toJSON() else null) for x in value)
when ListProperty, ObjectProperty, ReferenceProperty
value = ((if x? then x else null) for x in value)
else
value = ((if x? then new else null) for x in value)
value
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] = Array.apply(@, )
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
if ?
switch
when StringProperty
value = ((if x? then x.toString() else null) for x in value)
when IntegerProperty
value = ((if x? then parseInt(x) else null) for x in value)
when FloatProperty
value = ((if x? then parseFloat(x) else null) for x in value)
when BooleanProperty
value = ((if x? then Boolean(x) else null) for x in value)
when DateProperty
value = ((if x? then x.toJSON() else null) for x in value)
when ListProperty, ObjectProperty, ReferenceProperty
value = ((if x? then x else null) for x in value)
else
value = ((if x? then new else null) for x in value)
value
exports.ListProperty = ListProperty
class ObjectProperty extends Property
constructor: (args) ->
super args
toJs: (value) ->
if not value?
if ?
return util._extend({}, )
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
value
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] = util._extend({}, )
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
value
exports.ObjectProperty = ObjectProperty
class ReferenceProperty extends Property
constructor: (args={}) ->
{} = args
super args
toJs: (value) ->
if not value?
if ?
return
if
throw new exceptions.ValueRequiredError("#{@propertyName} is required.")
return null
if typeof(value) is 'string'
value
else if typeof(value) is 'object' and value.constructor is
value
else
throw new exceptions.TypeError("#{@propertyName} has wrong type.")
toDb: (classInstance) ->
value = classInstance[]
if not value?
if ?
classInstance[] =
return classInstance[]
if
throw new exceptions.ValueRequiredError("#{classInstance.constructor.name}.#{@propertyName} is required.")
return null
if typeof(value) is 'string'
value
else if typeof(value) is 'object' and value.constructor is
value.id
else
throw new exceptions.TypeError("#{classInstance.constructor.name}.#{@propertyName} has wrong type.")
exports.ReferenceProperty = ReferenceProperty