Class: Endeca::Document

Inherits:
Object show all
Extended by:
Transformer
Includes:
Readers
Defined in:
lib/endeca/document.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformer

map, transform_query_options

Methods included from Readers

included

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

#propertiesObject (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

#rawObject (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(query_options={})
  get_collection_class.new(request(query_options), 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, query_options={})
  first(query_options.merge(:id => id, :skip_default_endeca_parameters => true))
end

.field_namesObject



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, query_options={})
  case what
  when Integer, /^[A-Za-z\d]+$/
    by_id(what, query_options)
  when String
    all(what)
  when :first
    first(query_options)
  when :all
    all(query_options)
  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(query_options={})
  response = request(query_options)
  record = if response['AggrRecords'] 
    response['AggrRecords'].first['Records'].first
  elsif response['Records']
    response['Records'].first
  else
    nil
  end

  record && new(record)
end

.inspectObject



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

#dimensionsObject

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

#inspectObject



44
45
46
# File 'lib/endeca/document.rb', line 44

def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
end