Module: DocWrapper::ClassMethods

Defined in:
lib/doc_wrapper/base_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_property_accessor(property_name, wrapper) ⇒ Object



48
49
50
51
52
# File 'lib/doc_wrapper/base_class_methods.rb', line 48

def add_property_accessor (property_name, wrapper)
  define_method(property_name) do
    wrapper.property(documents)
  end
end

#add_property_definition(property_name, wrapper) ⇒ Object

Helper Methods #



38
39
40
41
# File 'lib/doc_wrapper/base_class_methods.rb', line 38

def add_property_definition (property_name, wrapper)
  add_property_name(property_name)
  add_property_accessor(property_name, wrapper)
end

#add_property_name(property_name) ⇒ Object

Add a property name to the singleton property_names attribute.



44
45
46
# File 'lib/doc_wrapper/base_class_methods.rb', line 44

def add_property_name (property_name)
  self.property_names << property_name
end

#build_property_definition(property_name, type, selector, options, block) ⇒ Object



54
55
56
# File 'lib/doc_wrapper/base_class_methods.rb', line 54

def build_property_definition (property_name, type, selector, options, block)
  DocWrapper.const_get("#{camelize(type.to_s)}PropertyDefinition").new(property_name, type, selector, initialize_options(options), block)
end

#camelize(string) ⇒ Object



58
59
60
# File 'lib/doc_wrapper/base_class_methods.rb', line 58

def camelize (string)
  string.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#has_many(property_name, selector, klass, options = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/doc_wrapper/base_class_methods.rb', line 16

def has_many (property_name, selector, klass, options = {})
  wrapper = HasManyPropertyDefinition.new(property_name, selector, klass, initialize_options(options))
  define_method property_name do
    wrapper.property(documents)
  end
end

#has_one(property_name, selector, klass, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/doc_wrapper/base_class_methods.rb', line 23

def has_one (property_name, selector, klass, options = {})
  wrapper = HasOnePropertyDefinition.new(property_name, selector, klass, initialize_options(options))
  define_method(property_name) do
    wrapper.property(documents)
  end
end

#initialize_options(options) ⇒ Object

Set default options that all properties need.



63
64
65
66
67
68
69
70
# File 'lib/doc_wrapper/base_class_methods.rb', line 63

def initialize_options (options)
  # Make sure the options have a :document key with a value of 1.
  # This forces all lookups to be for the 0th document in documents if
  # the user did not specify an offset into the array.
  options = { :document => 1 }.merge(options)
  options[:namespaces] = @namespaces if @namespaces
  options
end

#multi_property(property_name, selectors, options = {}, &block) ⇒ Object



11
12
13
14
# File 'lib/doc_wrapper/base_class_methods.rb', line 11

def multi_property (property_name, selectors, options = {}, &block)
  raise "Multi-properties require a block" if block.nil?
  add_property_definition(property_name, MultiPropertyDefinition.new(property_name, selectors, initialize_options(options), block))
end

#namespaces(namespaces) ⇒ Object



30
31
32
# File 'lib/doc_wrapper/base_class_methods.rb', line 30

def namespaces (namespaces)
  @namespaces = namespaces
end

#property(property_name, type, selector, options = {}, &block) ⇒ Object

Create a typed property definition for a document wrapper. The property_name must be a symbol.



6
7
8
9
# File 'lib/doc_wrapper/base_class_methods.rb', line 6

def property (property_name, type, selector, options = {}, &block)
  raise "Unhandled property type: #{type.to_s}" if ![:string, :date, :time, :boolean, :float, :raw].include?(type)
  add_property_definition(property_name,  build_property_definition(property_name, type, selector, initialize_options(options), block))
end