Sha256: f31d86d8dc728d140640954470928e956ceed3841c2699e4f1f7152b9ad42cd3

Contents?: true

Size: 981 Bytes

Versions: 13

Compression:

Stored size: 981 Bytes

Contents

require 'toy/proxies/embedded_list'

module Toy
  class EmbeddedList
    include Toy::Collection

    def after_initialize
      create_accessors
    end

    private
      def create_accessors
        model.class_eval """
          def #{name}
            #{instance_variable} ||= self.class.#{list_method}[:#{name}].new_proxy(self)
          end

          def #{name}=(records)
            #{name}.replace(records)
          end

          def #{name.to_s.singularize}_attributes=(attrs)
            self.#{name} = attrs.map do |value|
              value = value.is_a?(Hash) ? value : value[1]
              #{type}.new(value)
            end
          end

          def #{name.to_s.singularize}_attributes
            #{name}.map(&:attributes)
          end
        """

        type.class_eval { attr_accessor :parent_reference }
      end

      def proxy_class
        Toy::Proxies::EmbeddedList
      end

      def list_method
        :embedded_lists
      end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
toystore-0.8.3 lib/toy/embedded_list.rb
toystore-0.8.2 lib/toy/embedded_list.rb
toystore-0.8.1 lib/toy/embedded_list.rb
toystore-0.8.0 lib/toy/embedded_list.rb
toystore-0.7.0 lib/toy/embedded_list.rb
toystore-0.6.6 lib/toy/embedded_list.rb
toystore-0.6.5 lib/toy/embedded_list.rb
toystore-0.6.4 lib/toy/embedded_list.rb
toystore-0.6.3 lib/toy/embedded_list.rb
toystore-0.6.2 lib/toy/embedded_list.rb
toystore-0.6.1 lib/toy/embedded_list.rb
toystore-0.6 lib/toy/embedded_list.rb
toystore-0.5 lib/toy/embedded_list.rb