Module: Tire::Model::Persistence::Attributes::ClassMethods

Defined in:
lib/tire/model/persistence/attributes.rb

Instance Method Summary collapse

Instance Method Details

#propertiesObject


65
66
67
# File 'lib/tire/model/persistence/attributes.rb', line 65

def properties
  @properties ||= []
end

#property(name, options = {}) ⇒ Object

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.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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_defaultsObject


69
70
71
# File 'lib/tire/model/persistence/attributes.rb', line 69

def property_defaults
  @property_defaults ||= {}
end

#property_typesObject


73
74
75
# File 'lib/tire/model/persistence/attributes.rb', line 73

def property_types
  @property_types ||= {}
end