Sha256: 1eb96cdb63f3b5207d8e98c937db93bb2c68ac373d9eaa939c3c78253ee55af3
Contents?: true
Size: 1.36 KB
Versions: 1
Compression:
Stored size: 1.36 KB
Contents
# When included in a structure, this module turns it into a static # model, the data of which is sourced from a yaml file. # # This is a basic implementation and does not handle nested structures. # See test. class Structure module Static def self.included(base) base.key(:id, Integer) base.extend(ClassMethods) end module ClassMethods include Enumerable # The path for the data file. # # This file should contain a YAML representation of the records. # # Overwrite this reader with an opiniated location to dry. attr :data_path # Returns all records. def all @records ||= data.map do |record| record["id"] ||= increment_id new(record) end end # Yields each record to given block. # # Other enumerators will be made available by the Enumerable # module. def each(&block) all.each { |record| block.call(record) } end # Finds a record by its ID. def find(id) detect { |record| record.id == id } end # Sets the path for the data file. def set_data_path(data_path) @data_path = data_path end private def data YAML.load_file(@data_path) end def increment_id @increment_id = @increment_id.to_i + 1 end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
structure-0.15.0 | lib/structure/static.rb |