Method: Factbook::ProfileBuilder#initialize

Defined in:
lib/factbook-fields/builder.rb

#initialize(text_or_data) ⇒ ProfileBuilder

Returns a new instance of ProfileBuilder.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/factbook-fields/builder.rb', line 20

def initialize( text_or_data )

  data = if text_or_data.is_a?( String )
           text = text_or_data
           JSON.parse( text )
         else  ## assume it's already a hash
            text_or_data
         end


  @profile = Profile.new
  @errors  = []       ## fix/todo: sorry - for now no errors possible/tracked

  data.each do |k1,v1|
    category_title    = k1
    category_data     = v1

    category = Category.new( category_title )

    ## get fields
    category_data.each do |k2,v2|
      field_title = k2
      field_data  = v2

      field = Field.new( field_title )

      #####
      ## note: run data hash through normalize_title (again)
      if field_data.is_a?( Hash )
        new_field_data = {}
        field_data.each do |k3,v3|
          new_field_data[ normalize_title(k3) ] = v3
        end
        field_data = new_field_data
      end

      field.data  = field_data

      category << field
    end
    @profile << category
  end
end