Sha256: 7b07ecb623443e41b6171bd4c1b8419cb7dc01f637a6d3558042378c4430c95d
Contents?: true
Size: 1.79 KB
Versions: 2
Compression:
Stored size: 1.79 KB
Contents
module Handle class Record attr :fields def self.from_data(data) result = self.new if data.is_a?(String) data.lines.each { |line| result << Handle::Field::Base.from_data(line) } else data.each { |field| result << Handle::Field::Base.from_data(field) } end result end def initialize(fields=[]) initialize_with(fields) end def initialize_with(fields) @fields = fields end def to_a @fields end def to_json *args to_a.to_json *args end def to_s fields.collect(&:to_s).join("\n") end def <<(field) fields << field if field.kind_of?(Handle::Field::Base) end def add(field_type, value=nil) field = Handle::Field.const_get(field_type).new indexed_fields = fields.select { |f| f.index < 100 }.sort { |a,b| b.index <=> a.index } if indexed_fields.empty? field.index = 1 else field.index = indexed_fields.first.index + 1 end field.value = value fields << field field end def find_by_index(index) self.find { |field| field.index == index } end def |(other) result = { add: Record.new, update: Record.new, delete: Record.new } my_ixs = self.collect(&:index).sort other.each do |field| if my_ixs.delete(field.index) result[:update] << field else result[:add] << field end end my_ixs.each { |ix| result[:delete] << self.find_by_index(ix) } result end def ==(other) self.class == other.class && self.to_a == other.to_a end def method_missing(sym, *args, &block) if @fields.respond_to?(sym) @fields.send(sym, *args, &block) else super end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
handle-system-0.1.1 | lib/handle/record.rb |
handle-system-0.1.0 | lib/handle/record.rb |