Skip to content

Commit 2b74c05

Browse files
committedJun 3, 2014
[STORE] Added code annotations across the Persistence::Model codebase
1 parent 0c33d00 commit 2b74c05

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed
 

‎elasticsearch-persistence/lib/elasticsearch/persistence/model.rb

+27
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@
1111
module Elasticsearch
1212
module Persistence
1313

14+
# When included, extends a plain Ruby class with persistence-related features via the ActiveRecord pattern
15+
#
16+
# @example Include the repository in a custom class
17+
#
18+
# require 'elasticsearch/persistence/model'
19+
#
20+
# class MyObject
21+
# include Elasticsearch::Persistence::Repository
22+
# end
23+
#
1424
module Model
25+
26+
# Utility methods for {Elasticsearch::Persistence::Model}
27+
#
1528
module Utils
29+
30+
# Return Elasticsearch type based on passed Ruby class (used in the `attribute` method)
31+
#
1632
def lookup_type(type)
1733
case
1834
when type == String
@@ -49,6 +65,9 @@ def self.included(base)
4965
extend Elasticsearch::Persistence::Model::Find::ClassMethods
5066

5167
class << self
68+
69+
# Re-define the Virtus' `attribute` method, to configure Elasticsearch mapping as well
70+
#
5271
def attribute(name, type=nil, options={}, &block)
5372
mapping = options.delete(:mapping) || {}
5473
super
@@ -60,12 +79,16 @@ def attribute(name, type=nil, options={}, &block)
6079
gateway.mapping(&block) if block_given?
6180
end
6281

82+
# Return the {Repository::Class} instance
83+
#
6384
def gateway(&block)
6485
@gateway ||= Elasticsearch::Persistence::Repository::Class.new host: self
6586
block.arity < 1 ? @gateway.instance_eval(&block) : block.call(@gateway) if block_given?
6687
@gateway
6788
end
6889

90+
# Delegate methods to repository
91+
#
6992
delegate :settings,
7093
:mappings,
7194
:mapping,
@@ -79,6 +102,8 @@ def gateway(&block)
79102
to: :gateway
80103
end
81104

105+
# Configure the repository based on the model (set up index_name, etc)
106+
#
82107
gateway do
83108
klass base
84109
index_name base.model_name.collection.gsub(/\//, '-')
@@ -96,6 +121,8 @@ def deserialize(document)
96121
end
97122
end
98123

124+
# Set up common attributes
125+
#
99126
attribute :id, String, writer: :private
100127
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
101128
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }

‎elasticsearch-persistence/lib/elasticsearch/persistence/model/rails.rb

+17
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@ module Elasticsearch
22
module Persistence
33
module Model
44

5+
# Make the `Persistence::Model` models compatible with Ruby On Rails applications
6+
#
57
module Rails
68
def self.included(base)
79
base.class_eval do
10+
11+
# Decorates the passed in `attributes` so they extract the date & time values from Rails forms
12+
#
13+
# @example Correctly combine the date and time to a datetime string
14+
#
15+
# params = { "published_on(1i)"=>"2014",
16+
# "published_on(2i)"=>"1",
17+
# "published_on(3i)"=>"1",
18+
# "published_on(4i)"=>"12",
19+
# "published_on(5i)"=>"00"
20+
# }
21+
# MyRailsModel.new(params).published_on.iso8601
22+
# # => "2014-01-01T12:00:00+00:00"
23+
#
824
def initialize(attributes={})
925
day = attributes.select { |p| p =~ /\([1-3]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+'-'; sum }
1026
time = attributes.select { |p| p =~ /\([4-6]/ }.reduce({}) { |sum, item| (sum[item.first.gsub(/\(.+\)/, '')] ||= '' )<< item.last+':'; sum }
1127
unless day.empty? && time.empty?
1228
attributes.update day.reduce({}) { |sum, item| sum[item.first] = item.last + ' ' + time[item.first]; sum }
1329
end
30+
1431
super(attributes)
1532
end
1633
end

‎elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb

+96-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ module Elasticsearch
22
module Persistence
33
module Model
44

5+
# This module contains the storage related features of {Elasticsearch::Persistence::Model}
6+
#
57
module Store
6-
module ClassMethods
8+
module ClassMethods #:nodoc:
9+
10+
# Creates a class instance, saves it, if validations pass, and returns it
11+
#
12+
# @example Create a new person
13+
#
14+
# Person.create name: 'John Smith'
15+
# # => #<Person:0x007f889e302b30 ... @id="bG7yQDAXRhCi3ZfVcx6oAA", @name="John Smith" ...>
16+
#
17+
# @return [Object] The model instance
18+
#
719
def create(attributes, options={})
820
object = self.new(attributes)
921
object.run_callbacks :create do
@@ -14,6 +26,23 @@ def create(attributes, options={})
1426
end
1527

1628
module InstanceMethods
29+
30+
# Saves the model (if validations pass) and returns the response (or `false`)
31+
#
32+
# @example Save a valid model instance
33+
#
34+
# p = Person.new(name: 'John')
35+
# p.save
36+
# => {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>1, "created"=>true}
37+
#
38+
# @example Save an invalid model instance
39+
#
40+
# p = Person.new(name: nil)
41+
# p.save
42+
# # => false
43+
#
44+
# @return [Hash,FalseClass] The Elasticsearch response as a Hash or `false`
45+
#
1746
def save(options={})
1847
return false unless valid?
1948
run_callbacks :save do
@@ -25,6 +54,15 @@ def save(options={})
2554
end
2655
end
2756

57+
# Deletes the model from Elasticsearch (if it's persisted), freezes it, and returns the response
58+
#
59+
# @example Delete a model instance
60+
#
61+
# p.destroy
62+
# => {"_index"=>"people", ... "_id"=>"RzFSXFR0R8u1CZIWNs2Gvg", "_version"=>2 ...}
63+
#
64+
# @return [Hash] The Elasticsearch response as a Hash
65+
#
2866
def destroy(options={})
2967
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?
3068

@@ -37,6 +75,15 @@ def destroy(options={})
3775
end
3876
end; alias :delete :destroy
3977

78+
# Updates the model (via Elasticsearch's "Update" API) and returns the response
79+
#
80+
# @example Update a model with partial attributes
81+
#
82+
# p.update name: 'UPDATED'
83+
# => {"_index"=>"people", ... "_version"=>2}
84+
#
85+
# @return [Hash] The Elasticsearch response as a Hash
86+
#
4087
def update(attributes={}, options={})
4188
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?
4289

@@ -48,6 +95,18 @@ def update(attributes={}, options={})
4895
end
4996
end; alias :update_attributes :update
5097

98+
# Increments a numeric attribute (via Elasticsearch's "Update" API) and returns the response
99+
#
100+
# @example Increment the `salary` attribute by 1
101+
#
102+
# p.increment :salary
103+
#
104+
# @example Increment the `salary` attribute by 100
105+
#
106+
# p.increment :salary, 100
107+
#
108+
# @return [Hash] The Elasticsearch response as a Hash
109+
#
51110
def increment(attribute, value=1, options={})
52111
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?
53112

@@ -56,6 +115,18 @@ def increment(attribute, value=1, options={})
56115
response
57116
end
58117

118+
# Decrements a numeric attribute (via Elasticsearch's "Update" API) and returns the response
119+
#
120+
# @example Decrement the `salary` attribute by 1
121+
#
122+
# p.decrement :salary
123+
#
124+
# @example Decrement the `salary` attribute by 100
125+
#
126+
# p.decrement :salary, 100
127+
#
128+
# @return [Hash] The Elasticsearch response as a Hash
129+
#
59130
def decrement(attribute, value=1, options={})
60131
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?
61132

@@ -64,6 +135,18 @@ def decrement(attribute, value=1, options={})
64135
response
65136
end
66137

138+
# Updates the `updated_at` attribute, saves the model and returns the response
139+
#
140+
# @example Update the `updated_at` attribute (default)
141+
#
142+
# p.touch
143+
#
144+
# @example Update a custom attribute: `saved_on`
145+
#
146+
# p.touch :saved_on
147+
#
148+
# @return [Hash] The Elasticsearch response as a Hash
149+
#
67150
def touch(attribute=:updated_at, options={})
68151
raise DocumentNotPersisted, "Object not persisted: #{self.inspect}" unless persisted?
69152
raise ArgumentError, "Object does not have '#{attribute}' attribute" unless respond_to?(attribute)
@@ -76,14 +159,26 @@ def touch(attribute=:updated_at, options={})
76159
end
77160
end
78161

162+
# Returns true when the model has been destroyed, false otherwise
163+
#
164+
# @return [TrueClass,FalseClass]
165+
#
79166
def destroyed?
80167
!!@destroyed
81168
end
82169

170+
# Returns true when the model has been already saved to the database, false otherwise
171+
#
172+
# @return [TrueClass,FalseClass]
173+
#
83174
def persisted?
84175
!!@persisted && !destroyed?
85176
end
86177

178+
# Returns true when the model has not been saved yet, false otherwise
179+
#
180+
# @return [TrueClass,FalseClass]
181+
#
87182
def new_record?
88183
!persisted? && !destroyed?
89184
end

‎elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ def respond_to_missing?(method_name, *)
1717
end
1818
end
1919

20-
# When included, creates an instance of the {Repository::Class} class as a "gateway"
20+
# When included, creates an instance of the {Repository::Class Repository} class as a "gateway"
2121
#
2222
# @example Include the repository in a custom class
2323
#
24+
# require 'elasticsearch/persistence'
25+
#
2426
# class MyRepository
2527
# include Elasticsearch::Persistence::Repository
2628
# end

0 commit comments

Comments
 (0)
Please sign in to comment.