Class: Caster::Accessor
- Inherits:
-
Object
- Object
- Caster::Accessor
- Defined in:
- lib/caster/accessor.rb
Overview
Instance Method Summary collapse
- #access(doc_var, accessor) ⇒ Object
- #delete(doc, accessor) ⇒ Object
- #get(doc, accessor) ⇒ Object
- #set(doc, accessor, value) ⇒ Object
- #split(accessor) ⇒ Object
Instance Method Details
#access(doc_var, accessor) ⇒ Object
30 31 32 |
# File 'lib/caster/accessor.rb', line 30 def access doc_var, accessor doc_var << split(accessor).map { |field| "['#{field}']" }.join end |
#delete(doc, accessor) ⇒ Object
24 25 26 27 28 |
# File 'lib/caster/accessor.rb', line 24 def delete doc, accessor sub_fields = split(accessor) field_to_delete = sub_fields.pop eval("#{access('doc', sub_fields.join('.'))}.delete('#{field_to_delete}')") end |
#get(doc, accessor) ⇒ Object
9 10 11 12 13 |
# File 'lib/caster/accessor.rb', line 9 def get doc, accessor return doc if accessor == nil value = eval(access('doc', accessor)) (value.is_a? Fixnum)? value : value.clone end |
#set(doc, accessor, value) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/caster/accessor.rb', line 15 def set doc, accessor, value sub_doc = doc split(accessor).each do |sub_field| sub_doc[sub_field] = {} if sub_doc[sub_field] == nil sub_doc = sub_doc[sub_field] end eval("#{access('doc', accessor)} = value") end |
#split(accessor) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/caster/accessor.rb', line 34 def split accessor escape = false words = [] w = 0 (0..accessor.length - 1).each do |i| c = accessor[i].chr n = accessor[i+1].chr rescue '' if escape escape = false next end words[w] = '' if words[w] == nil if c == '\\' words[w] << n escape = true next end if c == '.' w = w + 1 next end words[w] << c end words end |