Module: Modis::Persistence::ClassMethods
- Defined in:
- lib/modis/persistence.rb
Constant Summary collapse
- YAML_MARKER =
'---'.freeze
Instance Method Summary collapse
- #absolute_namespace ⇒ Object
-
#bootstrap_sti(parent, child) ⇒ Object
:nodoc:.
- #create(attrs) ⇒ Object
- #create!(attrs) ⇒ Object
- #deserialize(record) ⇒ Object
- #key_for(id) ⇒ Object
- #namespace ⇒ Object
- #namespace=(value) ⇒ Object
Instance Method Details
#absolute_namespace ⇒ Object
42 43 44 |
# File 'lib/modis/persistence.rb', line 42 def absolute_namespace @absolute_namespace ||= [Modis.config.namespace, namespace].compact.join(':') end |
#bootstrap_sti(parent, child) ⇒ Object
:nodoc:
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/modis/persistence.rb', line 15 def bootstrap_sti(parent, child) child.instance_eval do parent.instance_eval do class << self attr_accessor :sti_parent end attribute :type, :string unless attributes.key?('type') end @sti_child = true @sti_parent = parent bootstrap_attributes(parent) bootstrap_indexes(parent) end end |
#create(attrs) ⇒ Object
50 51 52 53 54 |
# File 'lib/modis/persistence.rb', line 50 def create(attrs) model = new(attrs) model.save model end |
#create!(attrs) ⇒ Object
56 57 58 59 60 |
# File 'lib/modis/persistence.rb', line 56 def create!(attrs) model = new(attrs) model.save! model end |
#deserialize(record) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/modis/persistence.rb', line 63 def deserialize(record) values = record.values values = MessagePack.unpack(msgpack_array_header(values.size) + values.join) keys = record.keys values.each_with_index { |v, i| record[keys[i]] = v } record rescue MessagePack::MalformedFormatError found_yaml = false record.each do |k, v| if v.start_with?(YAML_MARKER) found_yaml = true record[k] = YAML.load(v) else record[k] = MessagePack.unpack(v) end end if found_yaml id = record['id'] STDERR.puts "#{self}(id: #{id}) contains attributes serialized as YAML. As of Modis 1.4.0, YAML is no longer used as the serialization format. To improve performance loading this record, you can force the record to new serialization format (MessagePack) with: #{self}.find(#{id}).save!(yaml_sucks: true)" end record end |
#key_for(id) ⇒ Object
46 47 48 |
# File 'lib/modis/persistence.rb', line 46 def key_for(id) "#{absolute_namespace}:#{id}" end |
#namespace ⇒ Object
32 33 34 35 |
# File 'lib/modis/persistence.rb', line 32 def namespace return sti_parent.namespace if sti_child? @namespace ||= name.split('::').map(&:underscore).join(':') end |
#namespace=(value) ⇒ Object
37 38 39 40 |
# File 'lib/modis/persistence.rb', line 37 def namespace=(value) @namespace = value @absolute_namespace = nil end |