Sha256: 568cb2c993079e1d7323e680341112effc27e8c593b8fb4b80464a827fb32e74

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Noodall
  class Site
    class << self

      attr_accessor :map, :permalinks

      def build!
        return false unless map.is_a?(Hash)
        map.each do |permalink, attributes|
          build_node(permalink, attributes)
        end
      end

      def contains?(permalink)
        self.permalinks ||= []
        return false unless map.is_a?(Hash)
        return true if map.keys.include?(permalink)
        map.values.each do |attrubutes|
          extract_permalinks(attrubutes)
        end
        return permalinks.include?(permalink)
      end

      private

      def extract_permalinks(attributes)
        children = attributes['children']
        if children
          self.permalinks = permalinks | children.keys
          children.values.each do |attributes|
            extract_permalinks(attributes)
          end
        end
      end

      def build_node(permalink, attr)
        attributes = attr.dup
        klass_name = attributes.delete('type')
        children = attributes.delete('children')

        node = Noodall::Node.first(:permalink => permalink) || create_node(klass_name, attributes.merge(:permalink => permalink))
        children.each do |permalink, attributes|
          build_node(permalink, attributes.merge(:parent => node))
        end if children
      end

      def create_node(klass_name, attributes)
        if defined?(Factory) && FactoryGirl.factories[klass_name.underscore.to_sym]
          Factory(klass_name.underscore, attributes)
        else
          klass_name.constantize.create!(attributes)
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
noodall-core-0.8.2 lib/noodall/site.rb
noodall-core-0.8.1 lib/noodall/site.rb