Module: Protoable::Persistence

Defined in:
lib/protobuf/activerecord/protoable/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/protobuf/activerecord/protoable/persistence.rb', line 3

def self.included(klass)
  klass.extend Protoable::Persistence::ClassMethods

  klass.class_eval do
    # Override Active Record's initialize method so it can accept a protobuf
    # message as it's attributes. Need to do it in class_eval block since initialize
    # is defined in ActiveRecord::Base.
    # :noapi:
    def initialize(*args)
      args[0] = attributes_from_proto(args.first) if args.first.is_a?(::Protobuf::Message)

      super(*args)
    end
  end
end

Instance Method Details

#assign_attributes(attributes, options = {}) ⇒ Object

:nodoc:



36
37
38
39
40
# File 'lib/protobuf/activerecord/protoable/persistence.rb', line 36

def assign_attributes(attributes, options = {})
  attributes = attributes_from_proto(proto) if attributes.is_a?(::Protobuf::Message)

  super(attributes, options)
end

#update_attributes(attributes, options = {}) ⇒ Object

:nodoc:



43
44
45
46
47
# File 'lib/protobuf/activerecord/protoable/persistence.rb', line 43

def update_attributes(attributes, options = {})
  attributes = attributes_from_proto(attributes) if attributes.is_a?(::Protobuf::Message)

  super(attributes, options)
end

#update_attributes!(attributes, options = {}) ⇒ Object

:nodoc:



50
51
52
53
54
# File 'lib/protobuf/activerecord/protoable/persistence.rb', line 50

def update_attributes!(attributes, options = {})
  attributes = attributes_from_proto(attributes) if attributes.is_a?(::Protobuf::Message)

  super(attributes, options)
end