Module: ExistDB::Meta

Included in:
IndexFactory::IndexFactory, XQLFactory::XQLFactory
Defined in:
lib/existdb/meta.rb

Instance Method Summary collapse

Instance Method Details

#initialize_with_options(options, ordered_options) ⇒ Object

Metaprogramming conviences



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/existdb/meta.rb', line 5

def initialize_with_options(options, ordered_options)
  # Usage: 
  #  class MyClass
  #    include Meta
  #    attr_accessor :opt1, :opt2
  #    def initialize(*options)
  #      initialize_with_options(options, [:opt1, :opt2])
  #    end
  #  end
  #
  # obj = MyClass.new(:opt1 => 'foo', :opt2 => 'bar')
  # # OR
  # obj = MyClass.new('foo', 'bar')
  # # MyClass.new now accepts named or ordered params!
  named_or_ordered_options(options, ordered_options).each do |key, value|
    if key.is_a?(Symbol) or key.is_a?(String) then
      key = "#{key}=".to_sym
      self.send(key, value) if self.respond_to?(key)
    end
  end
  
end

#named_or_ordered_options(options, ordered_options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/existdb/meta.rb', line 28

def named_or_ordered_options(options, ordered_options)
  hash = Hash.new
  options.each_with_index do |option, i|
    if option.is_a?(Hash) then
      hash.merge!(option)
    else
      key = ordered_options[i]
      hash[key] = option
    end
  end
  return hash
end