Class: Swiftype::BaseModel

Inherits:
OpenStruct
  • Object
show all
Includes:
Connection, Request
Defined in:
lib/swiftype/base_model.rb

Direct Known Subclasses

Document, DocumentType, Engine

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put

Methods included from Connection

#connection

Class Attribute Details

.parent_classesObject (readonly)

Returns the value of attribute parent_classes.



9
10
11
# File 'lib/swiftype/base_model.rb', line 9

def parent_classes
  @parent_classes
end

Class Method Details

.collection_nameObject



15
16
17
# File 'lib/swiftype/base_model.rb', line 15

def collection_name
  model_name.pluralize
end

.model_nameObject



11
12
13
# File 'lib/swiftype/base_model.rb', line 11

def model_name
  name.split('::').last.underscore
end

.parents(*parent_classes) ⇒ Object



19
20
21
# File 'lib/swiftype/base_model.rb', line 19

def parents(*parent_classes)
  @parent_classes = parent_classes
end

Instance Method Details

#create!Object



36
37
38
# File 'lib/swiftype/base_model.rb', line 36

def create!
  update_with! post(path_to_collection, {self.class.model_name => to_hash})
end

#destroy!Object



44
45
46
# File 'lib/swiftype/base_model.rb', line 44

def destroy!
  !!delete(path_to_model)
end

#idObject



24
25
26
# File 'lib/swiftype/base_model.rb', line 24

def id
  self._id
end

#identifierObject



85
86
87
# File 'lib/swiftype/base_model.rb', line 85

def identifier
  slugged? ? slug : id
end

#path_to_collectionObject



61
62
63
# File 'lib/swiftype/base_model.rb', line 61

def path_to_collection
  "#{raw_path_to_collection}.json"
end

#path_to_modelObject



48
49
50
# File 'lib/swiftype/base_model.rb', line 48

def path_to_model
  "#{raw_path_to_model}.json"
end

#raw_path_to_collectionObject



65
66
67
68
69
70
71
72
# File 'lib/swiftype/base_model.rb', line 65

def raw_path_to_collection
  path = (self.class.parent_classes || []).inject("") do |_, parent|
    parent_id = send("#{parent.model_name}_id")
    _ += "#{parent.collection_name}/#{parent_id}/"
    _
  end
  "#{path}#{self.class.collection_name}"
end

#raw_path_to_modelObject



52
53
54
55
56
57
58
59
# File 'lib/swiftype/base_model.rb', line 52

def raw_path_to_model
  path = (self.class.parent_classes || []).inject("") do |_, parent|
    parent_id = send("#{parent.model_name}_id")
    _ += "#{parent.collection_name}/#{parent_id}/"
    _
  end
  "#{path}#{self.class.collection_name}/#{identifier}"
end

#reloadObject



81
82
83
# File 'lib/swiftype/base_model.rb', line 81

def reload
  update_with! get(path_to_model)
end

#slugged?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/swiftype/base_model.rb', line 89

def slugged?
  respond_to?(:slug)
end

#to_hashObject



28
29
30
# File 'lib/swiftype/base_model.rb', line 28

def to_hash
  table
end

#to_jsonObject



32
33
34
# File 'lib/swiftype/base_model.rb', line 32

def to_json
  to_hash.to_json
end

#update!Object



40
41
42
# File 'lib/swiftype/base_model.rb', line 40

def update!
  update_with! put(path_to_model, {self.class.model_name => to_hash})
end

#update_with!(hash) ⇒ Object



74
75
76
77
78
79
# File 'lib/swiftype/base_model.rb', line 74

def update_with!(hash)
  hash.each do |k, v|
    send "#{k}=", v
  end
  self
end