Class: OccamsRecord::Results::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/occams-record/results.rb

Overview

Abstract class for result rows.

Like ActiveRecord, Boolean columns have #field? methods. However, unlike ActiveRecord, other column types do NOT.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_values) ⇒ Row

Initialize a new result row.

Parameters:

  • raw_values (Array)

    array of raw values from db



96
97
98
99
# File 'lib/occams-record/results.rb', line 96

def initialize(raw_values)
  @raw_values = raw_values
  @cast_values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/occams-record/results.rb', line 175

def method_missing(name, *args, &block)
  return super if args.any? or !block.nil?
  model = self.class.model_name.constantize

  if model.reflections.has_key? name.to_s
    raise MissingEagerLoadError.new(self, name)
  elsif model.columns_hash.has_key? name.to_s
    raise MissingColumnError.new(self, name)
  else
    super
  end
end

Class Attribute Details

.associationsObject

Array of associations names



80
81
82
# File 'lib/occams-record/results.rb', line 80

def associations
  @associations
end

.columnsObject

Array of column names



78
79
80
# File 'lib/occams-record/results.rb', line 78

def columns
  @columns
end

.model_nameObject

Name of Rails model



82
83
84
# File 'lib/occams-record/results.rb', line 82

def model_name
  @model_name
end

.primary_keyObject

Name of primary key column (nil if column wasn’t in the SELECT)



86
87
88
# File 'lib/occams-record/results.rb', line 86

def primary_key
  @primary_key
end

.table_nameObject

Name of originating database table



84
85
86
# File 'lib/occams-record/results.rb', line 84

def table_name
  @table_name
end

Instance Method Details

#==(obj) ⇒ Boolean

Returns true if the two objects are from the same table and have the same primary key.

Parameters:

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/occams-record/results.rb', line 117

def ==(obj)
  super ||
    obj.is_a?(OccamsRecord::Results::Row) &&
    obj.class.table_name && obj.class.table_name == self.class.table_name &&
    (pkey1 = obj.class.primary_key) && (pkey2 = self.class.primary_key) &&
    obj.send(pkey1) == self.send(pkey2)
end

#[](attr) ⇒ Object

Hash-like accessor for attributes and associations.

Parameters:

  • attr (String|Symbol\)

    ttr [String|Symbol\

Returns:

  • (Object)


107
108
109
# File 'lib/occams-record/results.rb', line 107

def [](attr)
  respond_to?(attr) ? send(attr) : nil
end

#inspectString

Returns a string with the “real” model name and raw result values.

Weird note - if this string is longer than 65 chars it won’t be used in exception messages. bugs.ruby-lang.org/issues/8982

Returns:

  • (String)


170
171
172
173
# File 'lib/occams-record/results.rb', line 170

def inspect
  id = self.class.primary_key ? send(self.class.primary_key) : "none"
  "#<#{self.class.model_name || "Anonymous"} #{self.class.primary_key}: #{id}>"
end

#to_h(symbolize_names: false, recursive: false) ⇒ Hash Also known as: to_hash

Return row as a Hash. By default the hash does NOT include associations.

Parameters:

  • symbolize_names (Boolean) (defaults to: false)

    if true, make Hash keys Symbols instead of Strings

  • recursive (Boolean) (defaults to: false)

    if true, include assiciations and them (and their associations) to hashes.

Returns:

  • (Hash)

    a Hash with String or Symbol keys



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/occams-record/results.rb', line 132

def to_h(symbolize_names: false, recursive: false)
  hash = self.class.columns.reduce({}) { |a, col_name|
    key = symbolize_names ? col_name.to_sym : col_name
    a[key] = send col_name
    a
  }

  recursive ? self.class.associations.reduce(hash) { |a, assoc_name|
    key = symbolize_names ? assoc_name.to_sym : assoc_name
    assoc = send assoc_name
    a[key] = if assoc.is_a? Array
               assoc.map { |x| x.to_h(symbolize_names: symbolize_names, recursive: true) }
             elsif assoc
               assoc.to_h(symbolize_names: symbolize_names, recursive: true)
             end
    a
  } : hash
end

#to_sString

Returns the name of the model and the attributes.

Returns:

  • (String)


158
159
160
# File 'lib/occams-record/results.rb', line 158

def to_s
  "#{self.class.model_name || "Anonymous"}#{to_h(symbolize_names: true, recursive: false)}"
end