class Semantic::Mapper::Markup

Constants

SELECTOR

Attributes

mapped_hash[R]

Public Class Methods

new(html) click to toggle source
# File lib/semantic/mapper/markup.rb, line 7
def initialize(html)
  @html = html
  @mapped_hash = HashWithIndifferentAccess.new
end

Public Instance Methods

map() click to toggle source
# File lib/semantic/mapper/markup.rb, line 12
def map
  document.css(SELECTOR).each do |element|
    fields(element).each do |key| 
      final_attribute_value = attribute_value(element, 'name')
      keys = build_keys(key)
      map_hash(final_attribute_value, keys)
    end 
  end
  @mapped_hash
end

Private Instance Methods

attribute_value(element, attribute_name) click to toggle source
# File lib/semantic/mapper/markup.rb, line 52
def attribute_value(element, attribute_name)
  element.attributes[attribute_name].try(:value)
end
build_keys(key) click to toggle source
# File lib/semantic/mapper/markup.rb, line 32
def build_keys(key)
  key.scan(/([\w-]+)|\[([\w-]+?)\]/).flatten.compact
end
document() click to toggle source
# File lib/semantic/mapper/markup.rb, line 48
def document
  @document ||= Nokogiri::HTML(@html)
end
fields(element) click to toggle source
# File lib/semantic/mapper/markup.rb, line 44
def fields(element)
  attribute_value(element, 'data-mapping').split(" ")
end
map_hash(value, keys) click to toggle source
# File lib/semantic/mapper/markup.rb, line 25
def map_hash(value, keys)
  merge_or_set(keys[0], value) and return if keys.length == 1 
  current_key = keys.pop
  value = HashWithIndifferentAccess.new(current_key => value)
  map_hash(value, keys)
end
merge_or_set(key, value) click to toggle source
# File lib/semantic/mapper/markup.rb, line 36
def merge_or_set(key, value)
  if @mapped_hash.has_key?(key)
    @mapped_hash[key].deep_merge!(value)
  else  
    @mapped_hash[key] = value
  end
end