Class: CSVSearch::Base
- Inherits:
-
Object
- Object
- CSVSearch::Base
- Defined in:
- lib/csv_search.rb
Class Attribute Summary collapse
-
.data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .all ⇒ Object
- .column_names ⇒ Object
- .find_by(opts) ⇒ Object
- .order(*columns) ⇒ Object
- .pluck(*columns) ⇒ Object
- .source(file_path) ⇒ Object
- .where(opts) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes ⇒ Object
-
#initialize(object) ⇒ Base
constructor
A new instance of Base.
- #values_at(*columns) ⇒ Object
Constructor Details
#initialize(object) ⇒ Base
Returns a new instance of Base.
40 41 42 43 44 45 46 47 48 |
# File 'lib/csv_search.rb', line 40 def initialize(object) object.each_pair do |key, value| next if self.class.column_names.exclude?(key.to_sym) define_singleton_method(key) do instance_variable_get("@#{key}") || instance_variable_set("@#{key}", value) end end end |
Class Attribute Details
.data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/csv_search.rb', line 7 def data @data end |
Class Method Details
.all ⇒ Object
19 20 21 |
# File 'lib/csv_search.rb', line 19 def all Relation.new(self).all end |
.column_names ⇒ Object
15 16 17 |
# File 'lib/csv_search.rb', line 15 def column_names @column_names ||= @data.first.keys.map(&:to_sym) end |
.find_by(opts) ⇒ Object
27 28 29 |
# File 'lib/csv_search.rb', line 27 def find_by(opts) Relation.new(self).find_by(opts) end |
.order(*columns) ⇒ Object
31 32 33 |
# File 'lib/csv_search.rb', line 31 def order(*columns) Relation.new(self).order(*columns) end |
.pluck(*columns) ⇒ Object
35 36 37 |
# File 'lib/csv_search.rb', line 35 def pluck(*columns) Relation.new(self).pluck(*columns) end |
.source(file_path) ⇒ Object
11 12 13 |
# File 'lib/csv_search.rb', line 11 def source(file_path) @data = CSV.foreach(file_path, headers: true).map(&:to_h) end |
.where(opts) ⇒ Object
23 24 25 |
# File 'lib/csv_search.rb', line 23 def where(opts) Relation.new(self).where(opts) end |
Instance Method Details
#==(other) ⇒ Object
58 59 60 61 62 |
# File 'lib/csv_search.rb', line 58 def ==(other) return false unless other.is_a?(self.class) values_at(*self.class.column_names) == other.values_at(*self.class.column_names) end |
#attributes ⇒ Object
50 51 52 |
# File 'lib/csv_search.rb', line 50 def attributes Hash[self.class.column_names.map { |column| [column, send(column)] }] end |
#values_at(*columns) ⇒ Object
54 55 56 |
# File 'lib/csv_search.rb', line 54 def values_at(*columns) columns.map { |column| send(column) } end |