Module: ArProtobufStore::ClassMethods

Defined in:
lib/ar_protobuf_store.rb

Instance Method Summary collapse

Instance Method Details

#protobuf_store(store_attribute, pb_class, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/ar_protobuf_store.rb', line 26

def protobuf_store(store_attribute, pb_class, options={})
  parser = ArProtobufStore.find_parser!(pb_class)
  serialize(store_attribute, parser)
  protobuf_store_accessor(store_attribute, parser.extract_fields(options[:accessors]))
end

#protobuf_store_accessor(store_attribute, *keys) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ar_protobuf_store.rb', line 32

def protobuf_store_accessor(store_attribute, *keys)
  Array(keys).flatten.each do |key|
    name = key[:name]
    coercer = case key[:type]
              when :int
                ".to_i"
              when :float
                ".to_f"
              when :string
                ".to_s"
              else
                ""
              end
    class_eval "      def \#{name}=(value)\n        self.\#{store_attribute}_will_change!\n        self.\#{store_attribute}.\#{name} = value\#{coercer}\n      end\n      def \#{name}\n        self.\#{store_attribute}.\#{name}\n      end\n    END_EVAL\n  end\nend\n", __FILE__, __LINE__