Sha256: 34c904a42fe1adb67d4ee0bbdf0a07f394c2a63cbfc983ff5255b5cbf54b4ffe

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module DashcodeConverter
  module Nib
    
    class View
    
      DECL_TEMPLATE= <<-EOF
          '<%=name%>': <%=is_template ? "VIEW_TEMPLATE" : "VIEW"%>({
          <%=views.join(",\n\n").indent(INDENT)%>
          })
      EOF
      
      attr_reader :name, :nib, :items, :items_by_id
      attr_accessor :is_template
      
      def initialize(name, spec, nib)
        @name= name
        @nib= nib
        @items= []
        @items_by_id= {}
        from_spec(spec) if spec
      end

      def remove_item(item)
        @items_by_id.delete(item.name)
        @items.delete(item)
      end
      
      def add_item(item)
        item.view= self
        @items_by_id[item.name]= item
        @items << item
      end
      
      def from_spec(spec)
        spec.each { |id, part_spec|
          item= NibItem.new("##{id}", part_spec, nib)
          add_item(item)
        }
      end
      
      def declaration
        return @declaration if @declaration
        
        views= items.map { |item| item.declaration }
        @declaration= ERB.new(DECL_TEMPLATE.remove_indent).result binding
      end
      
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dashcode-converter-0.0.1 lib/dashcode-converter/nib/view.rb