class DataFiles::ActiveData
Base class for data querying and manipulation.
Attributes
errors[R]
Public Class Methods
all()
click to toggle source
# File lib/data_files/active_data.rb, line 8 def self.all data.collect do |item| new(item) end end
attributes()
click to toggle source
# File lib/data_files/active_data.rb, line 58 def self.attributes class_variable_get(:@@attributes) end
data()
click to toggle source
# File lib/data_files/active_data.rb, line 50 def self.data class_variable_get(:@@data) end
data=(value)
click to toggle source
# File lib/data_files/active_data.rb, line 54 def self.data=(value) class_variable_set(:@@data, value) end
find_by(conditions)
click to toggle source
# File lib/data_files/active_data.rb, line 32 def self.find_by(conditions) results = where(conditions) if results.size.positive? results.first else nil end end
first()
click to toggle source
# File lib/data_files/active_data.rb, line 14 def self.first new(data.first) end
last()
click to toggle source
# File lib/data_files/active_data.rb, line 18 def self.last new(data.last) end
new(attrs = {})
click to toggle source
# File lib/data_files/active_data.rb, line 77 def initialize(attrs = {}) @_id = nil attrs.each { |key, value| send("#{key}=", value) } end
sort_by_primary_key()
click to toggle source
# File lib/data_files/active_data.rb, line 66 def self.sort_by_primary_key self.data = data.sort_by do |item| primary_key_value = item.values.first if primary_key_value.is_a? String primary_key_value.downcase else primary_key_value end end end
types()
click to toggle source
# File lib/data_files/active_data.rb, line 62 def self.types class_variable_get(:@@types) end
where(conditions)
click to toggle source
# File lib/data_files/active_data.rb, line 22 def self.where(conditions) all.select do |item| selected = true conditions.each do |key, value| selected = false if item.send(key) != value end selected end end
write_yaml()
click to toggle source
# File lib/data_files/active_data.rb, line 41 def self.write_yaml sort_by_primary_key item_attributes = all.collect(&:attributes) File.open("data/#{name.downcase}s.yml", 'w') do |file| file.write(item_attributes.to_yaml) end end
Public Instance Methods
attributes()
click to toggle source
# File lib/data_files/active_data.rb, line 82 def attributes attributes_hash = {} self.class.attributes.each do |attr| attributes_hash[attr] = send(attr) unless attr == '_id' end attributes_hash end
inspect()
click to toggle source
# File lib/data_files/active_data.rb, line 100 def inspect to_s end
save()
click to toggle source
# File lib/data_files/active_data.rb, line 129 def save return false unless valid? strip self.class.data = self.class.data.map do |item| if item['_id'] == @_id attributes.merge('_id' => @_id) else item end end if @_id.nil? @_id = next_id self.class.data << attributes.merge('_id' => @_id) end self.class.write_yaml true end
strip()
click to toggle source
# File lib/data_files/active_data.rb, line 151 def strip self.class.attributes.each do |attr| send("#{attr}=", send(attr).strip) if send(attr).is_a? String end self end
to_s()
click to toggle source
# File lib/data_files/active_data.rb, line 90 def to_s joined_attributes = self.class.attributes.collect do |attr| val = send(attr) val = "\"#{val}\"" if val.is_a? String "#{attr}: #{val.nil? ? 'nil' : val}" end.join(', ') "#<#{self.class} #{joined_attributes}>" end
valid?()
click to toggle source
# File lib/data_files/active_data.rb, line 104 def valid? @errors = [] primary_key = self.class.attributes.first primary_key_values = self.class.data.collect do |item| { item['_id'] => item[primary_key] } end primary_key_values.each do |item| item.each do |key, value| if key != @_id && value == send(primary_key) @errors << "#{self.class.name} with #{primary_key} #{send(primary_key)} already exists" end end end attributes.each do |key, value| unless self.class.types[key].include?(value.class.name) @errors << type_validation_error_message(key, self.class.types[key]) end end @errors.size.zero? end
Private Instance Methods
next_id()
click to toggle source
# File lib/data_files/active_data.rb, line 168 def next_id self.class.data.collect { |item| item['_id'] }.compact.max + 1 end
type_validation_error_message(attr, class_names)
click to toggle source
# File lib/data_files/active_data.rb, line 160 def type_validation_error_message(attr, class_names) allowed_types = class_names.map do |class_name| class_name.gsub('Class', '').downcase end "#{attr} must be #{allowed_types.sort.join(', ')}".sub(/.*\K, /, ' or ') end