Sha256: f542a2c680a1d575939ca1ab7e2442c5b59cfb836ee345a15b435e419196246f

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

module Rooftop
  module Content
    class Collection < ::Array
      def initialize(content_fields)
        content_fields.each do |field|
          # if the field has a 'fields' key, it is a repeater field. Collect the sub-fields and
          # set the field content to the collection of repeated fields
          if field.has_key?('fields')
            repeated_fields = field[:fields].collect do |repeated_fields|
              repeated_fields.collect{|field| Rooftop::Content::Field.new(field)}
            end
            
            field.delete(:fields)
            field[:value] = repeated_fields
          end

          self << Rooftop::Content::Field.new(field)
        end
      end

      # Find content_fields by attribute. Assume there will only be one attribute in the search
      def find_by(hash)
        raise ArgumentError, "you can only find a field by one attribute at a time" unless hash.length == 1
        attr = hash.first.first
        val = hash.first.last
        self.select {|l| l.send(attr) == val.to_s}
      end

      def named(name)
        find_by(name: name.to_s)
      end

      def field_names
        collect(&:name)
      end

      alias_method :names, :field_names

      def method_missing(method, *args, &block)
        fields = named(method)
        if fields.length > 0
          fields.first.value
        else
          raise Rooftop::Content::FieldNotFoundError, "No field named #{method} was found"
        end
      end

      def respond_to_missing?(method, private=false)
        if named(method).length == 0
          super
        else
          true
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rooftop-0.1.4.1 lib/rooftop/content/collection.rb
rooftop-0.1.4 lib/rooftop/content/collection.rb
rooftop-0.1.3 lib/rooftop/content/collection.rb
rooftop-0.1.2 lib/rooftop/content/collection.rb
rooftop-0.1.1 lib/rooftop/content/collection.rb