Module: SSDB::Attr::ClassMethods
- Defined in:
- lib/ssdb/attr.rb
Instance Attribute Summary collapse
-
#ssdb_attr_definition ⇒ Object
readonly
Returns the value of attribute ssdb_attr_definition.
-
#ssdb_attr_id_field ⇒ Object
readonly
Returns the value of attribute ssdb_attr_id_field.
-
#ssdb_attr_pool_name ⇒ Object
readonly
Returns the value of attribute ssdb_attr_pool_name.
Instance Method Summary collapse
-
#ssdb_attr(name, type, options = {}) ⇒ void
Method to define a SSDB attribute in a Ruby Class.
-
#ssdb_attr_id(field_name) ⇒ String
设置获取 SSDB Attr Id 的方式.
- #ssdb_attr_names ⇒ Object
-
#ssdb_attr_pool(pool_name) ⇒ String/Symbol
Specify which SSDB ConnectionPool current class should use, by name specified in
SSDBAttr.setup.
Instance Attribute Details
#ssdb_attr_definition ⇒ Object (readonly)
Returns the value of attribute ssdb_attr_definition.
14 15 16 |
# File 'lib/ssdb/attr.rb', line 14 def ssdb_attr_definition @ssdb_attr_definition end |
#ssdb_attr_id_field ⇒ Object (readonly)
Returns the value of attribute ssdb_attr_id_field.
15 16 17 |
# File 'lib/ssdb/attr.rb', line 15 def ssdb_attr_id_field @ssdb_attr_id_field end |
#ssdb_attr_pool_name ⇒ Object (readonly)
Returns the value of attribute ssdb_attr_pool_name.
16 17 18 |
# File 'lib/ssdb/attr.rb', line 16 def ssdb_attr_pool_name @ssdb_attr_pool_name end |
Instance Method Details
#ssdb_attr(name, type, options = {}) ⇒ void
This method returns an undefined value.
Method to define a SSDB attribute in a Ruby Class
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ssdb/attr.rb', line 54 def ssdb_attr(name, type, = {}) unless i(string integer).include?(type) raise "Type not supported, only `:string` and `:integer` are supported now." end @ssdb_attr_definition[name.to_s] = type.to_s define_method(name) do instance_variable_get("@#{name}") || begin val = ssdb_attr_pool.with { |conn| conn.get(ssdb_attr_key(name)) } || [:default] instance_variable_set("@#{name}", typecaster(val, type)) end end define_method("#{name}=") do |val| send("#{name}_will_change!") unless typecaster(val, type) == send(name) instance_variable_set("@#{name}", val) end define_method("#{name}_default_value") do typecaster([:default], type) end define_method("#{name}_was") { attribute_was(name) } define_method("#{name}_change") { attribute_change(name) } define_method("#{name}_changed?") { attribute_changed?(name) } define_method("restore_#{name}!") { restore_attribute!(name) } define_method("#{name}_will_change!") { attribute_will_change!(name) } end |
#ssdb_attr_id(field_name) ⇒ String
设置获取 SSDB Attr Id 的方式
25 26 27 28 |
# File 'lib/ssdb/attr.rb', line 25 def ssdb_attr_id(field_name) raise if field_name.nil? @ssdb_attr_id_field = field_name end |
#ssdb_attr_names ⇒ Object
41 42 43 |
# File 'lib/ssdb/attr.rb', line 41 def ssdb_attr_names @ssdb_attr_definition.keys end |
#ssdb_attr_pool(pool_name) ⇒ String/Symbol
Specify which SSDB ConnectionPool current class should use, by name specified in SSDBAttr.setup
37 38 39 |
# File 'lib/ssdb/attr.rb', line 37 def ssdb_attr_pool(pool_name) @ssdb_attr_pool_name = pool_name end |