module Ork::Model::ClassMethods
Attributes
Public Instance Methods
# File lib/ork/model/class_methods.rb, line 13 def attributes @attributes ||= [] end
# File lib/ork/model/class_methods.rb, line 17 def bucket Ork.riak.bucket(bucket_name) end
# File lib/ork/model/class_methods.rb, line 21 def bucket_name @bucket_name ||= self.to_s.downcase end
When a value is given, sets the content_type
of the object. When the parameter is nil, it will return the content_type. It also sets as default the standard content_type.
Example:
class User include Ork:Document content_type 'text/plain' end
# File lib/ork/model/class_methods.rb, line 52 def content_type(type = nil) @content_type = type unless type.nil? @content_type ||= 'application/json' end
Syntactic sugar for Model.new(atts)
.save
# File lib/ork/model/class_methods.rb, line 9 def create(atts = {}) new(atts).save end
# File lib/ork/model/class_methods.rb, line 37 def defaults @defaults ||= {} end
# File lib/ork/model/class_methods.rb, line 25 def embedding @embedding ||= [] end
# File lib/ork/model/class_methods.rb, line 29 def indices @indices ||= {} end
# File lib/ork/model/class_methods.rb, line 33 def uniques @uniques ||= [] end
Protected Instance Methods
Declares persisted attributes. All attributes are stored on the Riak hash.
Example:
class User include Ork::Document attribute :name end # It's the same as: class User include Ork::Document def name @attributes[:name] end def name=(name) @attributes[:name] = name end end
# File lib/ork/model/class_methods.rb, line 83 def attribute(name, options = {}) attributes << name unless attributes.include?(name) defaults[name] = options[:default] if options.has_key?(:default) if options.has_key?(:accessors) to_define = Array(options[:accessors]) & accessor_options else # Default methods to_define = [:reader, :writer] end to_define.each{|m| send("#{m}_for", name) } end
Index
any attribute on your model. Once you index an attribute, you can use it in ‘find` statements.
# File lib/ork/model/class_methods.rb, line 99 def index(name) indices[name] = Index.new(name) unless indices.include?(name) end
Create a ‘unique index’ for any method on your model. Actually it creates a regular index, but it checks if it’s repeated just before persist the new values.
Note: if there is a conflict while saving, an ‘Ork::UniqueIndexViolation` violation is raised.
# File lib/ork/model/class_methods.rb, line 110 def unique(attribute) uniques << attribute unless uniques.include?(attribute) index(attribute) end
Private Instance Methods
Valid options for attribute accessor value
# File lib/ork/model/class_methods.rb, line 119 def accessor_options [:reader, :writer, :question] end
Create question method
# File lib/ork/model/class_methods.rb, line 141 def question_for(name) define_method(:"#{name}?") do !!@attributes[name] end end
Create reader method
# File lib/ork/model/class_methods.rb, line 125 def reader_for(name) define_method(name) do @attributes[name] end end
Create writer method
# File lib/ork/model/class_methods.rb, line 133 def writer_for(name) define_method(:"#{name}=") do |value| @attributes[name] = value end end