模块:Mongoid::Extensions::String

定义于:
lib/mongoid/extensions/string.rb

Overview

为string类添加类型转换行为。

在命名空间下定义

模块: 类方法

实例属性摘要折叠

实例方法摘要折叠

实例属性详细信息

# unconvertable_to_bson对象

已弃用。


9
10
11
# File 'lib/mongoid/extensions/string.rb', line 9

def unconvertable_to_bson
  @unconvertable_to_bson
end

# unconvertable_to_bson 如果文档不可转换。 (如果文档不可转换。) ⇒对象

已弃用。


9
# File 'lib/mongoid/extensions/string.rb', line 9

attr_accessor :unconvertable_to_bson

实例方法详细信息

# __evolve_object_id__string | BSON::ObjectId

如果可能,将string转换为对象ID 。

例子:

演进string 。

"test".__evolve_object_id__

返回:

  • ( string | BSON::ObjectId )

    进化后的string 。



19
20
21
# File 'lib/mongoid/extensions/string.rb', line 19

def __evolve_object_id__
  convert_to_object_id
end

# __mongoize_object_id__string | BSON::ObjectId | nil

如果可能,将string Mongoize 为对象ID 。

例子:

演进string 。

"test".__mongoize_object_id__

返回:

  • ( string | BSON::ObjectId | nil )

    mongoized string 。



29
30
31
# File 'lib/mongoid/extensions/string.rb', line 29

def __mongoize_object_id__
  convert_to_object_id 除非 空白?
end

# __mongoize_time__时间| ActiveSupport::TimeWithZone

注意:

返回默认时区的本地时间。

将string Mongoize 进行存储。

例子:

将string Mongoize 。

"2012-01-01".__mongoize_time__
# => 2012-01-01 00:00:00 -0500

返回:

  • ( Time | ActiveSupport::TimeWithZone )

    与此string相对应的已配置默认时区域的本地时间。

引发:

  • ( ArgumentError )

    该string不是有效的时间string 。



45
46
47
48
49
50
51
52
# File 'lib/mongoid/extensions/string.rb', line 45

def __mongoize_time__
  # 需要这个额外的Time.parse 来引发错误,如果string
  # 不是有效的时间string 。 ActiveSupport::TimeZone 不
  # 执行此检查。
  ::时间.parse(self)

  ::时间.区域.parse(self)
end

# before_type_cast?true | false

string是否以 _before_type_cast 结尾?

例子:

string是 setter 方法吗?

"price_before_type_cast".before_type_cast?

返回:

  • ( true | false )

    如果字符串以 "_before_type_cast" 结尾



125
126
127
# File 'lib/mongoid/extensions/string.rb', line 125

def before_type_cast?
  Ends_with?(' _before_type_cast ')
end

# collectionizestring

将该string转换为集合友好名称。

例子:

集合string 。

"namespace/model".collectionize

返回:

  • ( string )

    集合友好形式的string 。



60
61
62
# File 'lib/mongoid/extensions/string.rb', line 60

def 集合化
  tableize.tr(' / ', ' _ ')
end

# mongoid_id?true | false

已弃用。

该string是 Mongoid ID的有效值吗?

例子:

该string是ID值吗?

"_id".mongoid_id?

返回:

  • ( true | false )

    如果string是ID或_id 。



71
72
73
# File 'lib/mongoid/extensions/string.rb', line 71

def mongoid_id?
  self =~ /\A(|_)id\z/
end

# numeric?true | false

Is the string a number?字面量“NaN”、“Infinity”和“-Infinity”都计为数字。

例子:

string是否为数字。

"1234.23".numeric?

返回:

  • ( true | false )

    如果string是数字。



83
84
85
86
87
# File 'lib/mongoid/extensions/string.rb', line 83

def numeric?
  !!Float(self)
救援 ArgumentError
  (self =~ / \A(?:NaN|-?Infinity)\z /) == 0
end

# Readerstring

将string作为 getter string获取。

例子:

获取读取器/获取器

"model=".reader

返回:

  • ( string )

    The string stripped of "=".



95
96
97
# File 'lib/mongoid/extensions/string.rb', line 95

def Reader
  删除(' = ').(/_before_type_cast\z/, ' ')
end

# unconvertable_to_bson?true | false

已弃用。

在创建条件时,对象是否不会转换为BSON ?

例子:

对象是否不可转换?

object.unconvertable_to_bson?

返回:

  • ( true | false )

    如果对象不可转换。



136
137
138
# File 'lib/mongoid/extensions/string.rb', line 136

def unconvertable_to_bson?
  @unconvertable_to_bson ||= false
end

# valid_method_name?true | false

此string是 valid_method_name 吗?

例子:

该string是否为用作方法名称的有效Ruby标识符

"model=".valid_method_name?

返回:

  • ( true | false )

    如果string包含有效的Ruby标识符。



115
116
117
# File 'lib/mongoid/extensions/string.rb', line 115

def valid_method_name?
  / [@$"-] / !~ self
end

# writer?true | false

此string是作者吗?

例子:

string是 setter 方法吗?

"model=".writer?

返回:

  • ( true | false )

    如果 string 包含“="。



105
106
107
# File 'lib/mongoid/extensions/string.rb', line 105

def writer?
  包括?(' = ')
end