Sha256: 20ef65bf4af543d8896a23d9f42b51d7fb478a0a5bdcb18769917d4b4975bc26
Contents?: true
Size: 1.41 KB
Versions: 3
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true require 'active_support/core_ext/string/inflections' class Domain class Model def initialize(attributes = {}) attributes.each do |k, v| key = "#{k}=" send(key, v) if respond_to?(key) end end # Convert instance attributes to a hash def to_hash self.class.columns.each_with_object({}) do |attr, hash| hash[attr] = send(attr.to_sym) end end def to_csv self.class.columns .map { |column| send(column.to_sym) } end def self.primary_key ['id'] end # Ensure the columns method retrieves only valid attribute names def self.columns instance_methods.select { |m| m.to_s.end_with?('=') } .map { |setter| setter.to_s.chomp('=') } .select { |attr| instance_methods.include?(attr.to_sym) && !Object.instance_methods.include?(attr.to_sym) } .sort end def self.headers columns.join ',' end def self.table_name name.underscore end def self.view "#{table_name}_view" end def self.from_json(data) transformed_data = data.transform_keys do |key| key.to_s.underscore.to_sym end new(transformed_data) end def self.from_csv(row) from_json(row.to_h) end def to_s self.class.columns.map { |attr| send(attr) }.join(',') end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
avs-0.1.3 | lib/domain/model.rb |
avs-0.1.2 | lib/domain/model.rb |
avs-0.1.1 | lib/domain/model.rb |