Module: Conflow::Redis::Model::ClassMethods Private

Defined in:
lib/conflow/redis/model.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Methods for defining fields on model

Constant Summary collapse

ALLOWED_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maps types (option for #field) to specific type field

{
  hash:       Conflow::Redis::HashField,
  array:      Conflow::Redis::ArrayField,
  value:      Conflow::Redis::ValueField,
  sorted_set: Conflow::Redis::SortedSetField,
  set:        Conflow::Redis::SetField,
  raw_value:  Conflow::Redis::RawValueField
}.freeze

Instance Method Summary collapse

Instance Method Details

#field(name, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Defines Redis field accessors.

Examples:

model_class.field :data, :hash
instance = model_class.new
instance.hash["something"] = 800
instance.hash = { something: "else"}
instance.hash["something"] #=> "else"

Parameters:

  • name (Symbol)

    name of the field

  • type (:hash, :array, :value, :sorted_set, :set)

    type of the new field

Raises:

  • (ArgumentError)

See Also:



77
78
79
80
81
82
83
# File 'lib/conflow/redis/model.rb', line 77

def field(name, type)
  type_class = ALLOWED_TYPES[type]
  raise ArgumentError, "Unknown type: #{type}. Should be one of: #{ALLOWED_TYPES.keys.inspect}" unless type_class

  fields << name
  FieldBuilder.new(name, type_class).call(self)
end

#fieldsArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns fields defined on this class.

Returns:

  • (Array<Symbol>)

    fields defined on this class



95
96
97
# File 'lib/conflow/redis/model.rb', line 95

def fields
  @fields ||= []
end

#has_many(name, klass, field_name: "#{name.to_s.chop}_ids") ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Convienience method for defining relation-like accessor.

Examples:

has_many :jobs, Conflow::Job # defines #job_ids and #jobs


88
89
90
91
92
# File 'lib/conflow/redis/model.rb', line 88

def has_many(name, klass, field_name: "#{name.to_s.chop}_ids")
  field(field_name, :array)
  relations << name
  define_method(name) { send(field_name).map { |id| klass.new(id) } }
end

#inherited(subclass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies field and relations information



57
58
59
60
# File 'lib/conflow/redis/model.rb', line 57

def inherited(subclass)
  subclass.instance_variable_set("@fields", fields.dup)
  subclass.instance_variable_set("@relations", relations.dup)
end

#relationsArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns relations defined on this class.

Returns:

  • (Array<Symbol>)

    relations defined on this class



100
101
102
# File 'lib/conflow/redis/model.rb', line 100

def relations
  @relations ||= []
end