Class: Sequel::Couchbase::Model
- Inherits:
-
Couchbase::Model
- Object
- Couchbase::Model
- Sequel::Couchbase::Model
- Defined in:
- lib/sequel_couchbase_model.rb
Constant Summary collapse
- @@plugins =
[]
Class Method Summary collapse
- .blank_object?(obj) ⇒ Boolean
- .db ⇒ Object
- .find(id) ⇒ Object
- .find!(id) ⇒ Object
- .plugin(plugin, *args, &block) ⇒ Object
- .plugin_module(plugin) ⇒ Object
- .plugin_module_defined?(plugin, submod) ⇒ Boolean
Instance Method Summary collapse
- #blank_object? ⇒ Boolean
- #disable_update_timestamps! ⇒ Object
- #errors ⇒ Object
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
- #valid? ⇒ Boolean
Class Method Details
.blank_object?(obj) ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sequel_couchbase_model.rb', line 58 def self.blank_object?(obj) return obj.blank? if obj.respond_to?(:blank?) case obj when NilClass, FalseClass true when Numeric, TrueClass false when String obj.strip.empty? else obj.respond_to?(:empty?) ? obj.empty? : false end end |
.db ⇒ Object
76 77 78 |
# File 'lib/sequel_couchbase_model.rb', line 76 def self.db self end |
.find(id) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/sequel_couchbase_model.rb', line 84 def self.find id begin super id rescue ::Couchbase::Error::NotFound nil end end |
.find!(id) ⇒ Object
80 81 82 |
# File 'lib/sequel_couchbase_model.rb', line 80 def self.find! id super id end |
.plugin(plugin, *args, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sequel_couchbase_model.rb', line 17 def self.plugin(plugin, *args, &block) m = plugin.is_a?(Module) ? plugin : plugin_module(plugin) unless @@plugins.include?(m) @@plugins << m m.apply(self, *args, &block) if m.respond_to?(:apply) include(m::InstanceMethods) if plugin_module_defined?(m, :InstanceMethods) extend(m::ClassMethods) if plugin_module_defined?(m, :ClassMethods) dataset_extend(m::DatasetMethods) if plugin_module_defined?(m, :DatasetMethods) end m.configure(self, *args, &block) if m.respond_to?(:configure) end |
.plugin_module(plugin) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sequel_couchbase_model.rb', line 39 def self.plugin_module(plugin) module_name = plugin.to_s.gsub(/(^|_)(.)/) { |x| x[-1..-1].upcase } if !Sequel::Plugins.const_defined?(module_name) || (Sequel.const_defined?(module_name) && Sequel::Plugins.const_get(module_name) == Sequel.const_get(module_name)) begin Sequel.tsk_require "sequel/plugins/#{plugin}" rescue LoadError => e begin Sequel.tsk_require "sequel_#{plugin}" rescue LoadError => e2 e. << "; #{e2.}" raise e end end end Sequel::Plugins.const_get(module_name) end |
.plugin_module_defined?(plugin, submod) ⇒ Boolean
29 30 31 32 33 34 35 36 37 |
# File 'lib/sequel_couchbase_model.rb', line 29 def self.plugin_module_defined?(plugin, submod) if RUBY_VERSION >= '1.9' plugin.const_defined?(submod, false) else # :nocov: plugin.const_defined?(submod) # :nocov: end end |
Instance Method Details
#blank_object? ⇒ Boolean
72 73 74 |
# File 'lib/sequel_couchbase_model.rb', line 72 def blank_object? self.class.blank_object? self end |
#disable_update_timestamps! ⇒ Object
106 107 108 |
# File 'lib/sequel_couchbase_model.rb', line 106 def @auto_timestamps_disabled = true end |
#errors ⇒ Object
110 111 112 |
# File 'lib/sequel_couchbase_model.rb', line 110 def errors @errors ||= Sequel::Model::Errors.new end |
#save(options = {}) ⇒ Object
96 97 98 |
# File 'lib/sequel_couchbase_model.rb', line 96 def save = {} save_ex , ->() { super }, ->() { false } end |
#save!(options = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/sequel_couchbase_model.rb', line 100 def save! = {} save_ex , ->() { super }, ->() { raise ValidationException.new(@errors), "Could not save #{self.class.name} model due to failing validation: #{errors.inspect}\n\tmodel was: #{inspect}" } end |
#valid? ⇒ Boolean
92 93 94 |
# File 'lib/sequel_couchbase_model.rb', line 92 def valid? errors.blank? end |