Module: EntityComponents::Hooks

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord, NoBrainer
Defined in:
lib/entity_components.rb

Instance Method Summary collapse

Instance Method Details

#initialize_componentsObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/entity_components.rb', line 9

def initialize_components
  components.each do |c|
    variable_name = "@#{c}".to_sym
    klass = c.camelize.constantize
    values = attributes["values"]
    name = c.to_sym

    define_singleton_method name do
      instance_variable_get(variable_name) || instance_variable_set(variable_name, klass.new(values[name]))
    end
  end
end

#serialize_componentsObject



22
23
24
25
26
27
# File 'lib/entity_components.rb', line 22

def serialize_components
  components.each do |c|
    name = c.to_sym
    values[name] = send(name).to_h
  end
end