Sha256: 5aa52b55903ce23b8ab7f226f1bdd53bb8b82700f0a5cdd44d5507c54aa84158

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

module Rumx
  class ListBean
    include Bean

    def initialize(list)
      @list = list
    end

    # Find the bean
    def bean_find(name_array, index = 0)
      return self if index == name_array.size
      name = name_array[index]
      return nil unless name.match(/^\d+$/)
      child = @list[name.to_i]
      return nil unless child
      return child.bean_find(name_array, index+1)
    end

    def bean_each_embedded_child(&block)
      @list.each_with_index do |child, i|
        yield i, child
      end
    end

    protected

    def do_bean_get_attributes(ancestry, &block)
      return do_bean_get_attributes_json unless block_given?
      child_ancestry = ancestry.dup
      # Save some object creation
      child_index = child_ancestry.size
      @list.each_with_index do |bean, i|
        child_ancestry[child_index] = i
        bean.bean_get_attributes(child_ancestry, &block)
      end
    end

    def do_bean_get_attributes_json
      @list.map { |bean| bean.bean_get_attributes }
    end

    def do_bean_set_attributes(params)
      return if !params || params.empty?
      changed = false
      @list.each_with_index do |bean, i|
        changed = true
        bean.bean_set_attributes(params[i] || params[i.to_s])
      end
      bean_attributes_changed if changed
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rumx-0.2.3 lib/rumx/list_bean.rb
rumx-0.2.2 lib/rumx/list_bean.rb
rumx-0.1.5 lib/rumx/list_bean.rb
rumx-0.1.4 lib/rumx/list_bean.rb
rumx-0.1.3 lib/rumx/list_bean.rb
rumx-0.1.2 lib/rumx/list_bean.rb
rumx-0.1.1 lib/rumx/list_bean.rb
rumx-0.1.0 lib/rumx/list_bean.rb