Class: ObjectifiedSessions::FieldDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/objectified_sessions/field_definition.rb

Overview

A FieldDefinition represents, well, the definition of a single field against a single class (which must be a descendant of ObjectifiedSessions::Base). It knows how to respond to a few questions, and is responsible for creating appropriate delegated methods in its owning class’s _dynamic_methods_module.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_class, name, options = { }) ⇒ FieldDefinition

Creates a new instance. session_class must be the Class that you’re using as your objectified-session class – i.e., a subclass of ObjectifiedSessions::Base. name is the name of the field. options are the options for the field:

:type

Required; must be one of: :normal, :retired, or :inactive, each corresponding to the field type documented in ObjectifiedSessions::Base.

:storage

If present, this field will use the specified string as the key under which it should be stored; if not present, the name will be used instead.

:visibility

Required; must be :private or :public. Methods created on the #_dynamic_methods_module on the base class will be of this visibility.

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'lib/objectified_sessions/field_definition.rb', line 30

def initialize(session_class, name, options = { })
  raise ArgumentError, "Session class must be a Class, not: #{session_class.inspect}" unless session_class.kind_of?(Class)

  @session_class = session_class
  @name = self.class.normalize_name(name)
  process_options!(options)

  create_methods!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/objectified_sessions/field_definition.rb', line 18

def name
  @name
end

#storage_nameObject (readonly)

Returns the key under which this field should read and write its data. This will be its name, unless a :storage option was passed to the constructor, in which case it will be that value, instead.



55
56
57
# File 'lib/objectified_sessions/field_definition.rb', line 55

def storage_name
  @storage_name
end

Class Method Details

.normalize_name(name) ⇒ Object

Normalizes the name of a field. We use this method to make sure we don’t get confused between Strings and Symbols, and so on.



9
10
11
12
13
14
15
# File 'lib/objectified_sessions/field_definition.rb', line 9

def normalize_name(name)
  unless name.kind_of?(String) || name.kind_of?(Symbol)
    raise ArgumentError, "A field name must be a String or Symbol, not: #{name.inspect}"
  end

  name.to_s.strip.to_sym
end

Instance Method Details

#==(other) ⇒ Object

Allow field comparison. We do this when you define a field that has the exact same name or storage name as another field – we allow it if (and only if) they are identical in every other way.



42
43
44
45
46
# File 'lib/objectified_sessions/field_definition.rb', line 42

def ==(other)
  return false unless other.kind_of?(ObjectifiedSessions::FieldDefinition)
  session_class == other.send(:session_class) && name == other.name && storage_name == other.storage_name &&
    type == other.send(:type) && visibility == other.send(:visibility)
end

#allow_access_to_data?Boolean

Should we allow users to access the data in this field? Retired and inactive fields don’t allow access to their data.

Returns:

  • (Boolean)


67
68
69
# File 'lib/objectified_sessions/field_definition.rb', line 67

def allow_access_to_data?
  type == :normal
end

#delete_data_with_storage_name?Boolean

If someone has set unknown_fields :delete on the base class, should we delete data with this field’s #storage_name anyway? This is true only for retired fields.

Returns:

  • (Boolean)


61
62
63
# File 'lib/objectified_sessions/field_definition.rb', line 61

def delete_data_with_storage_name?
  type == :retired
end

#eql?(other) ⇒ Boolean

Make sure eql? works the same as ==.

Returns:

  • (Boolean)


49
50
51
# File 'lib/objectified_sessions/field_definition.rb', line 49

def eql?(other)
  self == other
end