Sha256: 703859032fe6c4c84fab7665e36958c26d98973d09d4c0feda5cd30adf5a5a73

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

require 'push_type/primitives/base'
require 'push_type/primitives/string_type'
require 'push_type/primitives/number_type'
require 'push_type/primitives/array_type'
require 'push_type/primitives/object_type'

module PushType
  class FieldType

    class << self
      attr_reader :def_block, :init_block

      def options(opts = {})
        @options ||= opts
      end

      def on_class(&block)
        @def_block = block
      end

      def on_instance(&block)
        @init_block = block
      end
    end

    attr_reader :name, :model

    def initialize(name, model, opts = {})
      @name     = name
      @model    = model
      @opts     = [defaults, self.class.options, opts].compact.inject(&:deep_merge)
    end

    def kind
      self.class.name.demodulize.underscore.gsub(/_(field|type)$/, '').to_sym
    end

    def primitive
      begin
        "push_type/primitives/#{ json_primitive }_type".camelize.constantize
      rescue NameError
        "#{ json_primitive }_type".camelize.constantize
      end
    end

    def json_value
      model.field_store[name.to_s]
    end

    def value
      json_value
    end

    def json_primitive
      @opts[:json_primitive]
    end

    def css_class
      @opts[:css_class]
    end

    def template
      @opts[:template]
    end

    def label
      @opts[:label]
    end

    def form_helper
      @opts[:form_helper]
    end

    def html_options
      @opts[:html_options]
    end

    def field_options
      @opts[:field_options]
    end

    def multiple?
      @opts[:multiple]
    end

    private

    def defaults
      {
        json_primitive: :string,
        template:       'default',
        label:          name.to_s.humanize,
        form_helper:    :text_field,
        html_options:   {},
        field_options:  {},
        multiple:       false
      }
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
push_type_core-0.6.0.beta.3 lib/push_type/field_type.rb
push_type_core-0.6.0.beta.2 lib/push_type/field_type.rb