module Ork::Model::ClassMethods

Attributes

bucket_name[W]

Public Instance Methods

attributes() click to toggle source
# File lib/ork/model/class_methods.rb, line 13
def attributes
  @attributes ||= []
end
bucket() click to toggle source
# File lib/ork/model/class_methods.rb, line 17
def bucket
  Ork.riak.bucket(bucket_name)
end
bucket_name() click to toggle source
# File lib/ork/model/class_methods.rb, line 21
def bucket_name
  @bucket_name ||= self.to_s.downcase
end
content_type(type = nil) click to toggle source

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
create(atts = {}) click to toggle source

Syntactic sugar for Model.new(atts).save

# File lib/ork/model/class_methods.rb, line 9
def create(atts = {})
  new(atts).save
end
defaults() click to toggle source
# File lib/ork/model/class_methods.rb, line 37
def defaults
  @defaults ||= {}
end
embedding() click to toggle source
# File lib/ork/model/class_methods.rb, line 25
def embedding
  @embedding ||= []
end
indices() click to toggle source
# File lib/ork/model/class_methods.rb, line 29
def indices
  @indices ||= {}
end
uniques() click to toggle source
# File lib/ork/model/class_methods.rb, line 33
def uniques
  @uniques ||= []
end

Protected Instance Methods

attribute(name, options = {}) click to toggle source

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(name) click to toggle source

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
unique(attribute) click to toggle source

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

accessor_options() click to toggle source

Valid options for attribute accessor value

# File lib/ork/model/class_methods.rb, line 119
def accessor_options
  [:reader, :writer, :question]
end
question_for(name) click to toggle source

Create question method

# File lib/ork/model/class_methods.rb, line 141
def question_for(name)
  define_method(:"#{name}?") do
    !!@attributes[name]
  end
end
reader_for(name) click to toggle source

Create reader method

# File lib/ork/model/class_methods.rb, line 125
def reader_for(name)
  define_method(name) do
    @attributes[name]
  end
end
writer_for(name) click to toggle source

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