Class: Handle::Record
- Inherits:
-
Object
show all
- Defined in:
- lib/handle/record.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fields = []) ⇒ Record
Returns a new instance of Record.
15
16
17
|
# File 'lib/handle/record.rb', line 15
def initialize(fields=[])
initialize_with(fields)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/handle/record.rb', line 75
def method_missing(sym, *args, &block)
if @fields.respond_to?(sym)
@fields.send(sym, *args, &block)
else
super
end
end
|
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
3
4
5
|
# File 'lib/handle/record.rb', line 3
def fields
@fields
end
|
Class Method Details
.from_data(data) ⇒ Object
5
6
7
8
9
10
11
12
13
|
# File 'lib/handle/record.rb', line 5
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
|
Instance Method Details
#<<(field) ⇒ Object
35
36
37
|
# File 'lib/handle/record.rb', line 35
def <<(field)
fields << field if field.kind_of?(Handle::Field::Base)
end
|
#==(other) ⇒ Object
70
71
72
73
|
# File 'lib/handle/record.rb', line 70
def ==(other)
self.class == other.class &&
self.to_a == other.to_a
end
|
#add(field_type, value = nil) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/handle/record.rb', line 39
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
|
#find_by_index(index) ⇒ Object
52
53
54
|
# File 'lib/handle/record.rb', line 52
def find_by_index(index)
self.find { |field| field.index == index }
end
|
#initialize_with(fields) ⇒ Object
19
20
21
|
# File 'lib/handle/record.rb', line 19
def initialize_with(fields)
@fields = fields
end
|
#to_a ⇒ Object
23
24
25
|
# File 'lib/handle/record.rb', line 23
def to_a
@fields
end
|
#to_json(*args) ⇒ Object
27
28
29
|
# File 'lib/handle/record.rb', line 27
def to_json *args
to_a.to_json *args
end
|
#to_s ⇒ Object
31
32
33
|
# File 'lib/handle/record.rb', line 31
def to_s
fields.collect(&:to_s).join("\n")
end
|
#|(other) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/handle/record.rb', line 56
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
|