Class: Codec::Field
- Inherits:
-
Object
- Object
- Codec::Field
- Defined in:
- lib/codec/field.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_sub_field(sf) ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #get_deep_field(path, separator = '.') ⇒ Object
- #get_id ⇒ Object
- #get_sf_recursivly(ids) ⇒ Object
- #get_sub_field(id) ⇒ Object
- #get_sub_fields(id) ⇒ Object
- #get_value ⇒ Object
- #hash ⇒ Object
-
#initialize(id = "*", value = "") ⇒ Field
constructor
A new instance of Field.
- #search(path, separator = '.') ⇒ Object
- #set_deep_field(sf, path, separator = '.') ⇒ Object
- #set_id(id) ⇒ Object
- #set_node(value, new_value, path_ids) ⇒ Object
- #set_value(value, path = nil, separator = ".") ⇒ Object
Constructor Details
#initialize(id = "*", value = "") ⇒ Field
Returns a new instance of Field.
12 13 14 15 |
# File 'lib/codec/field.rb', line 12 def initialize(id="*",value="") @id = (id.nil? ? "*" : id) @value = value end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
144 145 146 |
# File 'lib/codec/field.rb', line 144 def id @id end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
144 145 146 |
# File 'lib/codec/field.rb', line 144 def value @value end |
Class Method Details
Instance Method Details
#==(other) ⇒ Object
26 27 28 |
# File 'lib/codec/field.rb', line 26 def ==(other) (@id == other.id && @value == other.value) end |
#add_sub_field(sf) ⇒ Object
64 65 66 67 68 |
# File 'lib/codec/field.rb', line 64 def add_sub_field(sf) @value = [] if @value == "" raise "Add sub field impossible on #{@value.class} value class" unless @value.kind_of? Array @value << [sf.id,sf.value] end |
#empty? ⇒ Boolean
17 18 19 |
# File 'lib/codec/field.rb', line 17 def empty? return true if @value == "" end |
#eql?(other) ⇒ Boolean
30 31 32 |
# File 'lib/codec/field.rb', line 30 def eql?(other) self == other end |
#get_deep_field(path, separator = '.') ⇒ Object
85 86 87 |
# File 'lib/codec/field.rb', line 85 def get_deep_field(path,separator='.') get_sf_recursivly(path.split(separator)) end |
#get_id ⇒ Object
38 |
# File 'lib/codec/field.rb', line 38 def get_id ; @id; end |
#get_sf_recursivly(ids) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/codec/field.rb', line 70 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
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/codec/field.rb', line 119 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
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/codec/field.rb', line 130 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 elsif sf_rec.size == 1 return [Field.new(id,sf_rec.first)] else sfs = [] sf_rec.each{|v| sfs << Field.new(id,v)} return sfs end end |
#get_value ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/codec/field.rb', line 42 def get_value if @value.kind_of?(Array) v = [] @value.each{|id,value| v << Field.new(id,value) } return v else return @value end end |
#hash ⇒ Object
34 35 36 |
# File 'lib/codec/field.rb', line 34 def hash @id.hash ^ @value.hash end |
#search(path, separator = '.') ⇒ Object
81 82 83 |
# File 'lib/codec/field.rb', line 81 def search(path,separator='.') get_sf_recursivly(path.split(separator)) end |
#set_deep_field(sf, path, separator = '.') ⇒ Object
114 115 116 117 |
# File 'lib/codec/field.rb', line 114 def set_deep_field(sf,path,separator='.') @value = set_node(@value,sf.value,path.split(separator)) self end |
#set_id(id) ⇒ Object
40 |
# File 'lib/codec/field.rb', line 40 def set_id id ; @id = id ; end |
#set_node(value, new_value, path_ids) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/codec/field.rb', line 89 def set_node(value,new_value,path_ids) is_set = false value = value.collect{|id,val| if id != path_ids.first [id,val] else is_set = true if path_ids.size == 1 [id,new_value] else [id,set_node(val,new_value,path_ids.slice(1,path_ids.size))] end end } unless is_set if path_ids.size == 1 value << [path_ids.first,new_value] else value << [path_ids.first,set_node_rec([],new_value, path_ids.slice(1,path_ids.size))] end end return value end |
#set_value(value, path = nil, separator = ".") ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/codec/field.rb', line 54 def set_value(value,path = nil,separator =".") if path.nil? raise "Error can not set value that is instance of Array" if value.kind_of? Array @value = value else @value = set_node(@value,value,path.split(separator)) end return self end |