Module: Dbla::Document

Included in:
Collection, Item
Defined in:
lib/dbla/document.rb

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

accessors for reading attribute



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dbla/document.rb', line 4

def [] *args
  if args.first && args.first.to_s.index('.')
    keys = args.first.to_s.split('.')
    r = keys.inject(_source) do |m,key|
      # Array() has a special behavior we do not want for hashes
      m = (m.is_a? Array) ? m : [m]
        m.inject([]) do |m2, a|
        if (a.respond_to? :[])
          r = a.send :[], *(args[1..-1].unshift(key))
          if r
            # Array() has a special behavior we do not want for hashes
            r = (r.is_a? Array) ? r : [r]
            m2 += r
          else
            m2
          end
        else
          m2
        end
      end
    end
    r.blank? ? nil : r
  else
    _source.send :[], *args
  end
end

#fetch(field, default) ⇒ Object



30
31
32
# File 'lib/dbla/document.rb', line 30

def fetch(field, default)
  (r = self[field]).blank? ? default : r
end

#has?(f, *values) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/dbla/document.rb', line 33

def has? f, *values
  if values.empty?
    !self[f].blank?
  else
    !(self[f] & values).blank?
  end
end