Sha256: e5c10e3b2e01ccd0d58a630b20989887f1ab4d58aef27d10eead4e97f62decce
Contents?: true
Size: 1.1 KB
Versions: 2
Compression:
Stored size: 1.1 KB
Contents
require 'yaml' require 'activesupport' class ActiveYAML @instances = [] def initialize(h) @h = h end def self.inherited(p) data = YAML.load_file("#{RAILS_ROOT}/db/yaml/#{p.name.underscore}.yml") p.class_eval do @instances = [] fields = [] for id, entry in data entry.merge!({'id' => id}) @instances << self.new(entry) for key, value in entry unless fields.include? key fields << key eval "def #{key}; @h[\"#{key}\"]; end" class_eval "def self.find_by_#{key}(x); @instances.find { |i| i.#{key} == x }; end" class_eval "def self.find_all_by_#{key}(x); @instances.find_all { |i| i.#{key} == x }; end" end end class_eval "def self.find(id); find_by_id(id) or raise ActiveRecord::RecordNotFound; end" class_eval "def self.first; @instances.first or raise ActiveRecord::RecordNotFound; end" class_eval "def self.last; @instances.last or raise ActiveRecord::RecordNotFound; end" end def self.find_all @instances end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ascheink-active_yaml-0.0.1 | lib/active_yaml.rb |
ascheink-active_yaml-0.0.2 | lib/active_yaml.rb |