Class: Vorpal::Config::ClassConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/vorpal/config/class_config.rb

Constant Summary collapse

ALLOWED_PRIMARY_KEY_TYPE_OPTIONS =
[:serial, :uuid]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ ClassConfig

Returns a new instance of ClassConfig.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vorpal/config/class_config.rb', line 13

def initialize(attrs)
  @has_manys = []
  @belongs_tos = []
  @has_ones = []
  @local_association_configs = []

  @serializer = attrs[:serializer]
  @deserializer = attrs[:deserializer]
  @domain_class = attrs[:domain_class]
  @db_class = attrs[:db_class]
  @primary_key_type = attrs[:primary_key_type]
  raise "Invalid primary_key_type: '#{@primary_key_type}'" unless ALLOWED_PRIMARY_KEY_TYPE_OPTIONS.include?(@primary_key_type)
end

Instance Attribute Details

#belongs_tosObject

Returns the value of attribute belongs_tos.



9
10
11
# File 'lib/vorpal/config/class_config.rb', line 9

def belongs_tos
  @belongs_tos
end

#db_classObject (readonly)

Returns the value of attribute db_class.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def db_class
  @db_class
end

#deserializerObject (readonly)

Returns the value of attribute deserializer.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def deserializer
  @deserializer
end

#domain_classObject (readonly)

Returns the value of attribute domain_class.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def domain_class
  @domain_class
end

#has_manysObject

Returns the value of attribute has_manys.



9
10
11
# File 'lib/vorpal/config/class_config.rb', line 9

def has_manys
  @has_manys
end

#has_onesObject

Returns the value of attribute has_ones.



9
10
11
# File 'lib/vorpal/config/class_config.rb', line 9

def has_ones
  @has_ones
end

#local_association_configsObject (readonly)

Returns the value of attribute local_association_configs.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def local_association_configs
  @local_association_configs
end

#primary_key_typeObject (readonly)

Returns the value of attribute primary_key_type.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def primary_key_type
  @primary_key_type
end

#serializerObject (readonly)

Returns the value of attribute serializer.



8
9
10
# File 'lib/vorpal/config/class_config.rb', line 8

def serializer
  @serializer
end

Instance Method Details

#build_db_object(attributes) ⇒ Object



27
28
29
# File 'lib/vorpal/config/class_config.rb', line 27

def build_db_object(attributes)
  db_class.new(attributes)
end

#deserialize(db_object) ⇒ Object



47
48
49
50
# File 'lib/vorpal/config/class_config.rb', line 47

def deserialize(db_object)
  attributes = get_db_object_attributes(db_object)
  serialization_required? ? deserializer.deserialize(domain_class.new, attributes) : db_object
end

#get_attribute(db_object, attribute) ⇒ Object



56
57
58
# File 'lib/vorpal/config/class_config.rb', line 56

def get_attribute(db_object, attribute)
  db_object.send(attribute)
end

#get_db_object_attributes(db_object) ⇒ Object



35
36
37
# File 'lib/vorpal/config/class_config.rb', line 35

def get_db_object_attributes(db_object)
  symbolize_keys(db_object.attributes)
end

#serialization_required?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/vorpal/config/class_config.rb', line 39

def serialization_required?
  domain_class.superclass.name != 'ActiveRecord::Base'
end

#serialize(object) ⇒ Object



43
44
45
# File 'lib/vorpal/config/class_config.rb', line 43

def serialize(object)
  serializer.serialize(object)
end

#set_attribute(db_object, attribute, value) ⇒ Object



52
53
54
# File 'lib/vorpal/config/class_config.rb', line 52

def set_attribute(db_object, attribute, value)
  db_object.send("#{attribute}=", value)
end

#set_db_object_attributes(db_object, attributes) ⇒ Object



31
32
33
# File 'lib/vorpal/config/class_config.rb', line 31

def set_db_object_attributes(db_object, attributes)
  db_object.attributes = attributes
end