Module: ArProtobufStore::ClassMethods

Defined in:
lib/ar_protobuf_store.rb

Instance Method Summary collapse

Instance Method Details

#protobuf_store(store_attribute, pb_class, options = nil) ⇒ Object



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

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

#protobuf_store_accessor(store_attribute, *keys) ⇒ Object



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

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