Module: Tire::Model::Persistence::Attributes::ClassMethods
- Defined in:
- lib/tire/model/persistence/attributes.rb
Instance Method Summary collapse
- #properties ⇒ Object
-
#property(name, options = {}) ⇒ Object
Define property of the model:.
- #property_defaults ⇒ Object
- #property_types ⇒ Object
Instance Method Details
#properties ⇒ Object
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, = {}) # 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 = .delete(:default)).nil? property_defaults[name.to_sym] = default_value end # Save property casting (when relevant): property_types[name.to_sym] = [:class] if [:class] # Store mapping for the property: mapping[name] = self end |
#property_defaults ⇒ Object
64 65 66 |
# File 'lib/tire/model/persistence/attributes.rb', line 64 def property_defaults @property_defaults ||= {} end |
#property_types ⇒ Object
68 69 70 |
# File 'lib/tire/model/persistence/attributes.rb', line 68 def property_types @property_types ||= {} end |