Module: Sequel::Plugins::UUIDPrefix::InstanceMethods
- Defined in:
- lib/sequel/plugins/uuid_prefix.rb
Instance Method Summary collapse
- #after_initialize ⇒ Object
- #before_create ⇒ Object
- #before_validation ⇒ Object
-
#canonical_uuid ⇒ Object
(also: #cuuid)
Returns canonicalized uuid which has the form of “#uuid_prefix-uuid”.
-
#to_api_document ⇒ Object
generate API response document.
- #to_hash ⇒ Object
-
#uuid_prefix ⇒ Object
read-only instance method to retrieve @uuid_prefix class variable.
Instance Method Details
#after_initialize ⇒ Object
83 84 85 86 87 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 83 def after_initialize super # set random generated uuid value self[:uuid] ||= Array.new(8) do UUID_TABLE[rand(UUID_TABLE.size)]; end.join end |
#before_create ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 75 def before_create if !self.class.find(:uuid=>self[:uuid]).nil? raise UUIDDuplication, "Duplicate UUID: #{self.canonical_uuid} already exists" end super end |
#before_validation ⇒ Object
68 69 70 71 72 73 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 68 def before_validation # trim uuid prefix if it is in the self[:uuid] self[:uuid].sub!(/^#{self.class.uuid_prefix}-/, '') super end |
#canonical_uuid ⇒ Object Also known as: cuuid
Returns canonicalized uuid which has the form of “#uuid_prefix-uuid”.
91 92 93 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 91 def canonical_uuid "#{self.uuid_prefix}-#{self[:uuid]}" end |
#to_api_document ⇒ Object
generate API response document. similar to to_hash() but not to expose integer primary key.
126 127 128 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 126 def to_api_document self.values.dup.merge({:id=>self.canonical_uuid, :uuid=>canonical_uuid}) end |
#to_hash ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 96 def to_hash() r = self.values.dup.merge({id: self.id, uuid: canonical_uuid, class_name: self.class.name.demodulize }) serialize_columns = [] if defined? Sequel::Plugins::Serialization if self.class.plugins.member?(Sequel::Plugins::Serialization) self.class.deserialization_map.keys.each { |c| serialize_columns << c r[c] = self.__send__(c) } end end # convert Sequel::SQL::Blob column. # TODO: look for alternative method to stop to retrieve # db_schema hash. self.class.db_schema.each { |c, v| if v[:db_type] == 'text' && v[:type] == :string && !serialize_columns.member?(c) r[c] = self.__send__(c).to_s end } r end |
#uuid_prefix ⇒ Object
read-only instance method to retrieve @uuid_prefix class variable.
64 65 66 |
# File 'lib/sequel/plugins/uuid_prefix.rb', line 64 def uuid_prefix self.class.uuid_prefix end |