module Tire::Model::Persistence::Attributes::ClassMethods

Public Instance Methods

properties() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 65
def properties
  @properties ||= []
end
property(name, options = {}) click to toggle source

Define property of the model:

class Article
  include Tire::Model::Persistence

  property :title,     :analyzer => 'snowball'
  property :published, :type => 'date'
  property :tags,      :analyzer => 'keywords', :default => []
end

You can pass mapping definition for Elasticsearch in the options Hash.

You can define default property values.

# File lib/tire/model/persistence/attributes.rb, line 26
def property(name, options = {})

  # Define attribute reader:
  define_method("#{name}") do
    instance_variable_get(:"@#{name}")
  end

  # Define attribute writer:
  define_method("#{name}=") do |value|
    instance_variable_set(:"@#{name}", value)
  end

  # Save the property in properties array:
  properties << name.to_s unless properties.include?(name.to_s)

  # Define convenience <NAME>? method:
  define_query_method      name.to_sym

  # ActiveModel compatibility. NEEDED?
  define_attribute_methods [name.to_sym]

  # Save property default value (when relevant):
  unless (default_value = options.delete(:default)).nil?
    property_defaults[name.to_sym] = default_value
  end

  # Save property casting (when relevant):
  property_types[name.to_sym] = options[:class] if options[:class]

  # Define default value for colletions:
  if options[:class].is_a?(Array)
    property_defaults[name.to_sym] ||= []
  end

  # Store mapping for the property:
  mapping[name] = { :type => 'string' }.merge(options)
  self
end
property_defaults() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 69
def property_defaults
  @property_defaults ||= {}
end
property_types() click to toggle source
# File lib/tire/model/persistence/attributes.rb, line 73
def property_types
  @property_types ||= {}
end