Class: Codec::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/codec/field.rb

Instance Method Summary collapse

Constructor Details

#initialize(id = "*") ⇒ Field

Returns a new instance of Field.



11
12
13
14
# File 'lib/codec/field.rb', line 11

def initialize(id="*")
  @id = (id.nil? ? "*" : id)
  @value = ""
end

Instance Method Details

#add_sub_field(sf) ⇒ Object



27
28
29
30
31
# File 'lib/codec/field.rb', line 27

def add_sub_field(sf)
  @value = [] if @value == ""
 raise "Add impossible on not Array valued field" unless @value.kind_of? Array
 @value << [sf.get_id,sf]
end

#get_deep_field(path, separator = '.') ⇒ Object



44
45
46
# File 'lib/codec/field.rb', line 44

def get_deep_field(path,separator='.')
	get_sf_recursivly(path.split(separator))
end

#get_idObject



16
# File 'lib/codec/field.rb', line 16

def get_id ; @id; end

#get_sf_recursivly(ids) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/codec/field.rb', line 33

def get_sf_recursivly(ids)
	if(ids.size == 1 && @value.kind_of?(Array))
		return get_sub_field(ids.first)
	elsif (ids.size > 1 && @value.kind_of?(Array))
		id = ids.slice!(0)
		return get_sub_field(id).get_sf_recursivly(ids)
	else
		return NilField.new
	end
end

#get_sub_field(id) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/codec/field.rb', line 48

def get_sub_field(id)
 sf_rec = get_sub_fields(id)
 if sf_rec.nil?
   return NilField.new
 elsif sf_rec.size > 1
   raise MultipleFieldError 
 else 
  return sf_rec.first
 end
end

#get_sub_fields(id) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/codec/field.rb', line 59

def get_sub_fields(id)
 raise "Error No Subfield" unless @value.kind_of? Array
 sf_rec = @value.select{|v| v.first == id}.collect{|v| v.last}
  if sf_rec == []
    return NilField.new
  else
    return sf_rec
  end
end

#get_valueObject



20
# File 'lib/codec/field.rb', line 20

def get_value ; @value; end

#set_id(id) ⇒ Object



18
# File 'lib/codec/field.rb', line 18

def set_id id ; @id = id ; end

#set_value(value) ⇒ Object



22
23
24
25
# File 'lib/codec/field.rb', line 22

def set_value(value)
 raise "Error can not set value that is instance of Array" if value.kind_of? Array
  @value = value
end

#to_yaml(tab = "") ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/codec/field.rb', line 69

def to_yaml(tab="")
  if @value.kind_of? Array
   s = tab + @id +": \n" 
   tab += "  "
   @value.each{|v|
     s += v.last.to_yaml(tab)
   }
   return s
 else
   tab + @id + ": " + @value.to_s + "\n"
 end
end