Class: DCA::Models::NokogiriBinder

Inherits:
Object
  • Object
show all
Includes:
BinderHelper
Defined in:
lib/dca/models/nokogiri_binder.rb

Class Method Summary collapse

Class Method Details

.parse(object, content, params) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dca/models/nokogiri_binder.rb', line 6

def self.parse object, content, params
  if params[:association] == :one
    parse_one object, content, params
  elsif params[:association] == :many
    parse_many object, content, params
  end
end

.parse_many(object, content, params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dca/models/nokogiri_binder.rb', line 28

def self.parse_many object, content, params
  result = nil

  options = params[:options] || {}
  selector = options[:selector]
  type = params[:type]
  type = find_type object, params[:field], options[:polymorphic] if type.nil? || options[:polymorphic]

  unless selector.nil?
    result = content.css(selector)
    result = object.send(options[:parser], result) unless options[:parser].nil?

    result = result.map { |node| type.new.bind node } unless result.nil?
  end
  result
end

.parse_one(object, content, params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dca/models/nokogiri_binder.rb', line 14

def self.parse_one object, content, params
  result = nil

  options = params[:options] || {}
  selector = options[:selector]
  element = selector.nil? ? content : content.at_css(selector)
  unless element.nil?
    result = options[:attribute].nil? ? element.content : element[options[:attribute]]
  end
  result = self.parse_options object, result, options

  convert result, params[:type]
end