Module: SSDB::Attr::ClassMethods

Defined in:
lib/ssdb/attr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ssdb_attr_definitionObject (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_fieldObject (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_nameObject (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

Parameters:

  • name (String/Symbol)

    Attribute name.

  • type (String/Symbol)

    Attribute type, now supports: string/integer

  • options (options) (defaults to: {})

    Extra options.



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, options = {})
  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)) } || options[: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(options[: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 的方式

Parameters:

  • field_name (String/Symbol)

Returns:

  • (String)


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_namesObject



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

Parameters:

  • pool_name (String/Symbol)

Returns:

  • (String/Symbol)


37
38
39
# File 'lib/ssdb/attr.rb', line 37

def ssdb_attr_pool(pool_name)
  @ssdb_attr_pool_name = pool_name
end