Class: Endeca::Document
Overview
Endeca Documents provide accessors for document properties returned by an Endeca query. Interesting Document properties must be declared with reader to be accessible on the object.
The reader
declaration provided by Readers can also handle basic data transformations (i.e. typecasting) and a few basic examples are provided (i.e. integer_reader
).
Instance Attribute Summary collapse
-
#properties ⇒ Object
(also: #attributes)
readonly
Returns the value of attribute properties.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
-
.all(query_options = {}) ⇒ Object
Returns all Documents matching the query options.
-
.by_id(id, query_options = {}) ⇒ Object
Returns a Document by id.
- .field_names ⇒ Object
-
.find(what, query_options = {}) ⇒ Object
Find operates with three distinct retrieval approaches:.
-
.first(query_options = {}) ⇒ Object
Returns the first Document matching the query options.
- .inspect ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#dimensions ⇒ Object
Returns the collection of Endeca::Dimension for the given Document.
-
#initialize(record_raw = nil) ⇒ Document
constructor
A new instance of Document.
- #inspect ⇒ Object
Methods included from Transformer
Methods included from Readers
Constructor Details
#initialize(record_raw = nil) ⇒ Document
Returns a new instance of Document.
23 24 25 26 |
# File 'lib/endeca/document.rb', line 23 def initialize(record_raw=nil) @raw = record_raw || {} @properties = @raw['Properties'] || {} end |
Instance Attribute Details
#properties ⇒ Object (readonly) Also known as: attributes
Returns the value of attribute properties.
22 23 24 |
# File 'lib/endeca/document.rb', line 22 def properties @properties end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
22 23 24 |
# File 'lib/endeca/document.rb', line 22 def raw @raw end |
Class Method Details
.all(query_options = {}) ⇒ Object
Returns all Documents matching the query options.
118 119 120 |
# File 'lib/endeca/document.rb', line 118 def self.all(={}) get_collection_class.new(request(), self) end |
.by_id(id, query_options = {}) ⇒ Object
Returns a Document by id
123 124 125 |
# File 'lib/endeca/document.rb', line 123 def self.by_id(id, ={}) first(.merge(:id => id, :skip_default_endeca_parameters => true)) end |
.field_names ⇒ Object
18 |
# File 'lib/endeca/document.rb', line 18 def self.field_names; reader_names; end |
.find(what, query_options = {}) ⇒ Object
Find operates with three distinct retrieval approaches:
-
Find by id - This is a specific id (1) or id string (“1”)
-
Find first - This will return the first record matching the query options used
-
Find all - This will return a collection of Documents matching the query options. This is the default behavior of find if only query options are passed.
Parameters
Find accepts a query options hash. These options are either passed directly to Endeca or mapped (by use of map
) to new parameters that are passed to Endeca.
Examples
# find by id
Listing.find(1) # returns the Document for ID = 1
Listing.find('1') # returns the Document for ID = 1
# find all
Listing.find(:all) # returns a collection of Documents
Listing.find(:all, :available => true)
# find first
Listing.find(:first) # Returns the first Document for the query
Listing.find(:first, :available => true)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/endeca/document.rb', line 87 def self.find(what, ={}) case what when Integer, /^[A-Za-z\d]+$/ by_id(what, ) when String all(what) when :first first() when :all all() else all(what) end end |
.first(query_options = {}) ⇒ Object
Returns the first Document matching the query options.
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/endeca/document.rb', line 104 def self.first(={}) response = request() record = if response['AggrRecords'] response['AggrRecords'].first['Records'].first elsif response['Records'] response['Records'].first else nil end record && new(record) end |
.inspect ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/endeca/document.rb', line 34 def self.inspect return <<-INSPECT #<#{self}> Path: #{get_path.inspect} Collection Class: #{get_collection_class.inspect}" Mappings:\n\t#{mappings.collect{|k,v| "#{k}: #{v.inspect}\n\t"}.to_s} DefaultParams:\n\t#{get_default_params.collect{|k,v| "#{k}: #{v.inspect}\n\t"}.to_s} INSPECT end |
Instance Method Details
#==(other) ⇒ Object
30 31 32 |
# File 'lib/endeca/document.rb', line 30 def ==(other) id == other.id end |
#dimensions ⇒ Object
Returns the collection of Endeca::Dimension for the given Document
49 50 51 52 53 54 55 56 57 |
# File 'lib/endeca/document.rb', line 49 def dimensions return @dimensions if @dimensions @dimensions = {} (raw['Dimensions'] || {}).each do |name, values| values = [values] unless Array === values @dimensions[name] = values.map{|value| Dimension.new(value)} end @dimensions end |
#inspect ⇒ Object
44 45 46 |
# File 'lib/endeca/document.rb', line 44 def inspect "#<#{self.class}:0x#{self.object_id.to_s(16)}>" end |