Sha256: ae5f8685699389412af9df3edfe3b803cfbeacfdce36231b3a5e738649a7d6d6

Contents?: true

Size: 957 Bytes

Versions: 33

Compression:

Stored size: 957 Bytes

Contents

module PushType
  class StructureField < PushType::FieldType

    options json_primitive: :object,
            template:       'structure'

    def initialize(*args, &block)
      super
      structure_class.class_eval(&block) if block
    end

    def value
      structure.field_store = json_value unless json_value.blank?
      structure
    end

    def fields
      structure.fields
    end

    private

    def structure_class
      @structure_class ||= begin
        (@opts[:class] || name).to_s.classify.constantize
      rescue NameError
        Class.new PushType::Structure do
          define_singleton_method(:name) { "PushType::Structure" }
        end
      end
    end

    def structure
      @structure ||= structure_class.new
    end

    on_class do |klass, field_name, field_class|
      klass.validate do |n|
        unless n.send(field_name).valid?
          n.errors.add field_name, :invalid
        end
      end
    end

  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
push_type_core-0.12.1 app/fields/push_type/structure_field.rb
push_type_core-0.12.0 app/fields/push_type/structure_field.rb
push_type_core-0.12.0.beta.1 app/fields/push_type/structure_field.rb
push_type_core-0.11.2 app/fields/push_type/structure_field.rb
push_type_core-0.11.1 app/fields/push_type/structure_field.rb
push_type_core-0.11.0.beta.2 app/fields/push_type/structure_field.rb
push_type_core-0.11.0.beta.1 app/fields/push_type/structure_field.rb
push_type_core-0.10.4 app/fields/push_type/structure_field.rb
push_type_core-0.10.3 app/fields/push_type/structure_field.rb
push_type_core-0.10.2 app/fields/push_type/structure_field.rb
push_type_core-0.10.1 app/fields/push_type/structure_field.rb
push_type_core-0.10.0 app/fields/push_type/structure_field.rb
push_type_core-0.10.0.beta.5 app/fields/push_type/structure_field.rb
push_type_core-0.10.0.beta.3 app/fields/push_type/structure_field.rb
push_type_core-0.9.5 app/fields/push_type/structure_field.rb
push_type_core-0.9.3 app/fields/push_type/structure_field.rb
push_type_core-0.9.2 app/fields/push_type/structure_field.rb
push_type_core-0.9.1 app/fields/push_type/structure_field.rb
push_type_core-0.9.0 app/fields/push_type/structure_field.rb
push_type_core-0.9.0.beta.4 app/fields/push_type/structure_field.rb