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

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

Instance Method Summary collapse

Instance Method Details

#propertiesObject



60
61
62
# File 'lib/tire/model/persistence/attributes.rb', line 60

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
# 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]

  # Store mapping for the property:
  mapping[name] = options
  self
end

#property_defaultsObject



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

def property_defaults
  @property_defaults ||= {}
end

#property_typesObject



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

def property_types
  @property_types ||= {}
end