Class: LucidWorks::Base
- Inherits:
-
Object
- Object
- LucidWorks::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming, ActiveModel::Translation, SimpleNaming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks, Associations, Utils::BoolConverter
- Defined in:
- lib/lucid_works/base.rb
Overview
LucidWorks::Base is our REST ORM foundation.
class Collection < LucidWorks::Base
end
When creating or retrieving objects, a parent LucidWorks::Base or LucidWorks::Server object must be supplied, e.g.
server = LucidWorks::Server.new('http://localhost:8888')
collection = Collection.find('collection1', :parent => server)
datasource = Datasource.find(1, :parent => collection)
This mechanism is used to build up the URI that will be used to manipulate the object in the REST API. For the above example:
datasource.uri -> http://localhost:8888/api/collections/collection1/datasources/1
If you are accessing models using associations, the parent argument is taken care of for you:
datasource = server.collection('collection1').datasource(1)
Direct Known Subclasses
Activity, Activity::History, Activity::Status, Collection, Collection::Click, Collection::Index, Collection::Info, Collection::Settings, Crawler, Datasource, Datasource::Crawldata, Datasource::History, Datasource::Index, Datasource::Job, Datasource::Schedule, Datasource::Status, Field, Fieldtype, Jdbcdriver, Logs, Logs::Index, Logs::Index::Summary, Logs::Query, Logs::Query::Summary, Role, Version
Constant Summary
Constants included from Utils::BoolConverter
Utils::BoolConverter::FALSE_VALUES, Utils::BoolConverter::TRUE_VALUES
Class Attribute Summary collapse
-
.collection_name ⇒ Object
:nodoc:.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
:nodoc:.
-
#id ⇒ Object
:nodoc:.
-
#parent ⇒ Object
:nodoc:.
-
#persisted ⇒ Object
writeonly
:nodoc:.
-
#raw_response ⇒ Object
:nodoc:.
-
#response_data ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.all(options) ⇒ Object
Shortcut for find(:all, options).
-
.collection_url(parent) ⇒ Object
:nodoc:.
-
.create(*arguments) ⇒ Object
Create a new model.
-
.extract_parent_from_options(options) ⇒ Object
:nodoc:.
-
.find(*arguments) ⇒ Object
Retrieve one or more models from the server.
- .first(options) ⇒ Object
-
.human_attribute_value(attribute, value) ⇒ Object
Convert the attribute value to a string.
- .last(options) ⇒ Object
-
.schema(&block) ⇒ Object
The attributes for a model are ascertained in on of two ways.
-
.singleton_name ⇒ Object
:nodoc:.
-
.to_select(attribute) ⇒ Object
For attributes listed in the schema as having :values, this will create an array-of-arrays suitable for use as options_for_select.
Instance Method Summary collapse
-
#collection_url ⇒ Object
:nodoc:.
- #destroy(options = {}) ⇒ Object
-
#human_attribute_value(attribute) ⇒ Object
Convert the attribute value to a string.
-
#initialize(options) ⇒ Base
constructor
class methods.
-
#inspect ⇒ Object
:nodoc:.
-
#member_url ⇒ Object
(also: #uri)
:nodoc:.
- #persisted? ⇒ Boolean
-
#read_attribute_for_validation(key) ⇒ Object
:nodoc:.
- #save ⇒ Object
- #update_attributes(attrs_and_values) ⇒ Object
Methods included from SimpleNaming
Methods included from Utils::BoolConverter
Constructor Details
#initialize(options) ⇒ Base
class methods
288 289 290 291 292 293 294 295 |
# File 'lib/lucid_works/base.rb', line 288 def initialize() raise ArgumentError.new("new requires a Hash") unless .is_a?(Hash) @parent = self.class.() @associations = {} @persisted = .delete(:persisted) || singleton? || false @attributes = {}.with_indifferent_access load_attributes() end |
Class Attribute Details
.collection_name ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/lucid_works/base.rb', line 49 def collection_name @collection_name end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
:nodoc:
44 45 46 |
# File 'lib/lucid_works/base.rb', line 44 def attributes @attributes end |
#id ⇒ Object
:nodoc:
353 354 355 |
# File 'lib/lucid_works/base.rb', line 353 def id # :nodoc: @attributes[self.class.schema.primary_key] end |
#parent ⇒ Object
:nodoc:
39 40 41 |
# File 'lib/lucid_works/base.rb', line 39 def parent @parent end |
#persisted=(value) ⇒ Object (writeonly)
:nodoc:
41 42 43 |
# File 'lib/lucid_works/base.rb', line 41 def persisted=(value) @persisted = value end |
#raw_response ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/lucid_works/base.rb', line 42 def raw_response @raw_response end |
#response_data ⇒ Object
:nodoc:
43 44 45 |
# File 'lib/lucid_works/base.rb', line 43 def response_data @response_data end |
Class Method Details
.all(options) ⇒ Object
Shortcut for find(:all, options)
229 230 231 |
# File 'lib/lucid_works/base.rb', line 229 def all() find(:all, ) end |
.collection_url(parent) ⇒ Object
:nodoc:
264 265 266 |
# File 'lib/lucid_works/base.rb', line 264 def collection_url(parent) # :nodoc: "#{parent.uri}/#{collection_name}" end |
.create(*arguments) ⇒ Object
Create a new model.
MyObject.create(attr => value, ..., :parent => server_or_object)
80 81 82 |
# File 'lib/lucid_works/base.rb', line 80 def create(*arguments) new(*arguments).tap { |model| model.save } end |
.extract_parent_from_options(options) ⇒ Object
:nodoc:
272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/lucid_works/base.rb', line 272 def () # :nodoc: # When this resource belongs_to another, allow the other's name as a key instead of :parent if respond_to?(:belongs_to_association_name) && .has_key?(belongs_to_association_name) parent = .delete(belongs_to_association_name) else parent = .delete(:parent) end raise ArgumentError.new("parent is a required option (options were #{.inspect}") unless parent unless parent.is_a?(Base) || parent.is_a?(Server) raise ArgumentError.new("parent must be a LucidWorks::Server or LucidWorks::Base") end parent end |
.find(*arguments) ⇒ Object
Retrieve one or more models from the server. Find may be called in the following ways:
Retrieve an entire collection:
find(:all, )
Retrieve a single model
find(id, )
find(:one, id, )
Retrieve a singleton model
find()
find(:singleton, )
Options
:parent - mandatory, another LucidWorks::Base instance or a LucidWorks::Server instance.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/lucid_works/base.rb', line 102 def find(*arguments) unless arguments.first.is_a?(Symbol) # We weren't called with a symbol, figure out what kind of find this is and re-call if singleton raise ArgumentError.new("wrong number of arguments (#{arguments.size} for 1)") unless arguments.size == 1 return find(:singleton, *arguments) else raise ArgumentError.new("wrong number of arguments (#{arguments.size} for 2)") unless arguments.size == 2 return find(:one, *arguments) end end kind_of_find = arguments.shift if kind_of_find == :one id = arguments.shift = arguments.shift raise ArgumentError.new("find(:one) requires 3 arguments") if arguments.count > 0 else = arguments.first end parent = () includes = .delete(:include) order = .delete(:order) url = case kind_of_find when :all; collection_url(parent) when :one; "#{parent.uri}/#{collection_name}/#{id}" when :singleton; "#{parent.uri}/#{singleton_name}" end raw_response = ActiveSupport::Notifications.instrument("lucid_works.request") do |payload| begin payload[:method] = :get payload[:uri] = url payload[:response] = RestClient.get(url) rescue RestClient::Exception => exception # Tack on what we were doing when we got the exception, then send it on up exception. = "#{exception.} while performing #{payload[:method]} #{payload[:uri]}" raise exception end end data = JSON.parse(raw_response) results = if kind_of_find == :all data.collect do |model_attributes| new(model_attributes.merge(:parent => parent, :persisted => true)) end else attributes = data.is_a?(Hash) ? data : {} attributes.merge!(:parent => parent, :persisted => true) object = new(attributes) object.raw_response = raw_response object.response_data = data object end # Process :include options # # In the section, to reduce confusion we will use the terms owner/target instead of # parent/child to describe the association. # # If we are doing a find(:all) and this owner resource supports retrieval of all targets # in one request with owners/all/targets, get them and attach them to the original models' # association proxys. # results_array = [results].flatten # allow us to process a single result or array with the same code if includes && !results_array.empty? includes = [includes].flatten includes.each do |association_name| association_info = associations[association_name.to_s.singularize.to_sym] target_class = class_eval(association_info[:class_name]) # get scoping right target_name = association_info[:class_name].underscore if kind_of_find == :all && association_info[:retrieveable_en_masse] all_targets_path = "#{collection_url(parent)}/all/#{target_name}" raw_response = ActiveSupport::Notifications.instrument("lucid_works.request") do |payload| begin payload[:method] = :get payload[:uri] = all_targets_path payload[:response] = RestClient.get(all_targets_path) rescue RestClient::Exception => exception # Tack on what we were doing when we got the exception, then send it on up exception. = "#{exception.} while performing #{payload[:method]} #{payload[:uri]}" raise exception end end if association_info[:type] == :has_one all_targets_attributes = JSON.parse raw_response all_targets_attributes.each do |target_attributes| owner_id = target_attributes['id'] owner = results_array.detect { |result| result.id == owner_id } if owner target = target_class.new(target_attributes.merge(:parent => owner, :persisted => true)) association_proxy = owner.send(association_name) association_proxy.target = target end end elsif association_info[:type] == :has_many # [{"history":[...history_models...],"id":372},{"history":[...history_models...],"id":371}] JSON.parse(raw_response).each do |group_of_targets| owner_id = group_of_targets['id'] owner = results_array.detect { |result| result.id == owner_id } targets = group_of_targets[target_name].collect do |target_attrs| target_class.new(target_attrs.merge(:parent => owner, :persisted => true)) end association_proxy = owner.send(association_name) association_proxy.target = targets end end else # kind_of_find != :all || !retrieveable_en_masse results_array.each do |result| result.send("#{association_name}!") end end end end results.sort! { |a,b| a.send(order) <=> b.send(order) } if order results end |
.first(options) ⇒ Object
233 234 235 |
# File 'lib/lucid_works/base.rb', line 233 def first() find(:all, ).first end |
.human_attribute_value(attribute, value) ⇒ Object
Convert the attribute value to a string. If a schema has been defined for the model and a type has been defined for the attribute, it will have formatting applied as follows:
-
boolean
will be converted to ‘yes’ or ‘no’ -
integer
will be passed to number_with_delimter
If the attributes is listed in the schema as having :values =>, it will be translated.
249 250 251 |
# File 'lib/lucid_works/base.rb', line 249 def human_attribute_value(attribute, value) schema[attribute].human_value(value) end |
.last(options) ⇒ Object
237 238 239 |
# File 'lib/lucid_works/base.rb', line 237 def last() find(:all, ).last end |
.schema(&block) ⇒ Object
The attributes for a model are ascertained in on of two ways. Without a schema, the attributes list is automatically generated when the the object is retrieved from the server. Alternatively, you may define a schema for your object. Objects with attributes defined by schemas may have validations run against those attributes during creation.
Schema should be passed a block:
class Collection < LucidWorks::Base
schema do
attribute :name
end
validates_presence_of :name
end
67 68 69 70 71 72 73 74 |
# File 'lib/lucid_works/base.rb', line 67 def schema(&block) @schema ||= LucidWorks::Schema.new(self) if block_given? @schema.instance_eval(&block) @schema.create_accessors_for_attributes(self) end @schema end |
.singleton_name ⇒ Object
:nodoc:
268 269 270 |
# File 'lib/lucid_works/base.rb', line 268 def singleton_name # :nodoc: name.underscore.gsub(/^.*\//, '') end |
.to_select(attribute) ⇒ Object
For attributes listed in the schema as having :values, this will create an array-of-arrays suitable for use as options_for_select.
256 257 258 |
# File 'lib/lucid_works/base.rb', line 256 def to_select(attribute) schema[attribute].to_select end |
Instance Method Details
#collection_url ⇒ Object
:nodoc:
369 370 371 |
# File 'lib/lucid_works/base.rb', line 369 def collection_url # :nodoc: self.class.collection_url(parent) end |
#destroy(options = {}) ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/lucid_works/base.rb', line 334 def destroy(={}) ActiveSupport::Notifications.instrument("lucid_works.request") do |payload| begin payload[:method] = :delete payload[:uri] = member_url payload[:options] = payload[:repsonse] = RestClient.delete(member_url, ) rescue RestClient::Conflict => e payload[:exception] = e attach_errors_to_model(e.response) false rescue RestClient::Exception => exception # Tack on what we were doing when we got the exception, then send it on up exception. = "#{exception.} while performing #{payload[:method]} #{payload[:uri]}" raise exception end end end |
#human_attribute_value(attribute) ⇒ Object
Convert the attribute value to a string. See LucidWorks::Base.human_attribute_values for details.
390 391 392 |
# File 'lib/lucid_works/base.rb', line 390 def human_attribute_value(attribute) self.class.human_attribute_value(attribute, send(attribute)) end |
#inspect ⇒ Object
:nodoc:
383 384 385 |
# File 'lib/lucid_works/base.rb', line 383 def inspect # :nodoc: "<#{self.class.name} " + @attributes.map { |k,v| "#{k}=#{v.inspect}" }.join(" ") + ">" end |
#member_url ⇒ Object Also known as: uri
:nodoc:
373 374 375 376 377 378 379 |
# File 'lib/lucid_works/base.rb', line 373 def member_url # :nodoc: if singleton? "#{parent.uri}/#{self.class.singleton_name}" else "#{parent.uri}/#{collection_name}/#{self.id}" end end |
#persisted? ⇒ Boolean
361 362 363 |
# File 'lib/lucid_works/base.rb', line 361 def persisted? @persisted end |
#read_attribute_for_validation(key) ⇒ Object
:nodoc:
365 366 367 |
# File 'lib/lucid_works/base.rb', line 365 def read_attribute_for_validation(key) # :nodoc: @attributes[key] end |
#save ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/lucid_works/base.rb', line 297 def save return false unless valid? _run_save_callbacks do ActiveSupport::Notifications.instrument("lucid_works.request") do |payload| method, uri = persisted? ? [:put, member_url] : [:post, collection_url] data = encode payload[:method] = method payload[:uri] = uri payload[:data] = data begin response = RestClient.send(method, uri, data, :content_type => :json) payload[:response] = response @persisted = true load_attributes_from_json_string(response) true rescue RestClient::Conflict, # 409 RestClient::UnprocessableEntity, # 422 RestClient::InternalServerError => e # 500 payload[:exception] = e attach_errors_to_model(e.response) false rescue RestClient::Exception => exception # Tack on what we were doing when we got the exception, then send it on up exception. = "#{exception.} while performing #{payload[:method]} #{payload[:uri]}" raise exception end end end end |
#update_attributes(attrs_and_values) ⇒ Object
327 328 329 330 331 332 |
# File 'lib/lucid_works/base.rb', line 327 def update_attributes(attrs_and_values) attrs_and_values.each do |attr,value| self.send("#{attr}=", value) end save end |