class ArDocStore::Attributes::EmbedsOne

Private Instance Methods

create_attributes_method() click to toggle source
# File lib/ar_doc_store/attributes/embeds_one.rb, line 44
      def create_attributes_method
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{attribute}_attributes=(values={})
          values.symbolize_keys! if values.respond_to?(:symbolize_keys!)
          if values[:_destroy] && (values[:_destroy] == '1')
            self.#{attribute} = nil
          else
            item = ensure_#{attribute}
            item.attributes = values
            item
          end
        end
        CODE
      end
create_build_method() click to toggle source

TODO: figure out how to set parent on read and write

# File lib/ar_doc_store/attributes/embeds_one.rb, line 28
      def create_build_method
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def build_#{attribute}(attributes=nil)
          self.#{attribute} = #{class_name}.build(attributes, self)
        end
        CODE
      end
create_ensure_method() click to toggle source
# File lib/ar_doc_store/attributes/embeds_one.rb, line 36
      def create_ensure_method
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def ensure_#{attribute}
          #{attribute} || build_#{attribute}
        end
        CODE
      end
create_validation() click to toggle source
# File lib/ar_doc_store/attributes/embeds_one.rb, line 59
      def create_validation
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
          def validate_embedded_record_for_#{attribute}
            validate_embeds_one :#{attribute}
          end
          validate :validate_embedded_record_for_#{attribute}
        CODE
      end
store_attribute() click to toggle source
# File lib/ar_doc_store/attributes/embeds_one.rb, line 8
      def store_attribute
        model.class_eval <<-CODE, __FILE__, __LINE__ + 1
        attribute :#{attribute}, ArDocStore::Types::EmbedsOne.new("#{@class_name}")
        def #{attribute}
          value = read_attribute :#{attribute}
          value && value.parent = self
          value && value.embedded_as = :#{attribute}
          value
        end
        def #{attribute}=(value)
          value = nil if value == '' || value == ['']
          write_attribute :#{attribute}, value
          value && #{attribute}.parent = self
          value && #{attribute}.embedded_as = :#{attribute}
          #{attribute}
        end
        CODE
      end