Class: DigitalOpera::Document::Fields::Standard

Inherits:
Object
  • Object
show all
Defined in:
lib/digital_opera/document/fields/standard.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Standard

Create the new field with a name and optional additional options.

Examples:

Create the new field.

Field.new(:name, :type => String)

Parameters:

  • options (Hash) (defaults to: {})

    The field options.

Options Hash (options):

  • :type (Class)

    The class of the field.

  • :default (Object)

    The default value for the field.

  • :label (String)

    The field’s label.

Since:

  • 3.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/digital_opera/document/fields/standard.rb', line 43

def initialize(name, options = {})
  @name = name
  @options = options
  @label = options[:label]
  @default_val = options[:default]

  # @todo: Durran, change API in 4.0 to take the class as a parameter.
  # This is here temporarily to address #2529 without changing the
  # constructor signature.
  if default_val.respond_to?(:call)
    define_default_method(options[:klass])
  end
end

Instance Attribute Details

#default_valObject

Defines the behaviour for defined fields in the document. Set readers for the instance variables.



7
8
9
# File 'lib/digital_opera/document/fields/standard.rb', line 7

def default_val
  @default_val
end

#labelObject

Defines the behaviour for defined fields in the document. Set readers for the instance variables.



7
8
9
# File 'lib/digital_opera/document/fields/standard.rb', line 7

def label
  @label
end

#nameObject

Defines the behaviour for defined fields in the document. Set readers for the instance variables.



7
8
9
# File 'lib/digital_opera/document/fields/standard.rb', line 7

def name
  @name
end

#optionsObject

Defines the behaviour for defined fields in the document. Set readers for the instance variables.



7
8
9
# File 'lib/digital_opera/document/fields/standard.rb', line 7

def options
  @options
end

Instance Method Details

#eval_default(doc) ⇒ Object

Evaluate the default value and return it. Will handle the serialization, proc calls, and duplication if necessary.

Examples:

Evaluate the default value.

field.eval_default(document)

Parameters:

  • doc (Document)

    The document the field belongs to.

Returns:

  • (Object)

    The serialized default value.

Since:

  • 2.1.8



23
24
25
26
27
28
29
# File 'lib/digital_opera/document/fields/standard.rb', line 23

def eval_default(doc)
  if fields = Threaded.selection(doc.criteria_instance_id)
    evaluated_default(doc) if included?(fields)
  else
    evaluated_default(doc)
  end
end

#pre_processed?true, false

Does the field pre-process its default value?

Examples:

Does the field pre-process the default?

field.pre_processed?

Returns:

  • (true, false)

    If the field’s default is pre-processed.

Since:

  • 3.0.0



65
66
67
68
# File 'lib/digital_opera/document/fields/standard.rb', line 65

def pre_processed?
  @pre_processed ||=
    (options[:pre_processed] || (default_val && !default_val.is_a?(::Proc)))
end

#typeClass

Get the type of this field - inferred from the class name.

Examples:

Get the type.

field.type

Returns:

  • (Class)

    The name of the class.

Since:

  • 2.1.0



78
79
80
# File 'lib/digital_opera/document/fields/standard.rb', line 78

def type
  @type ||= options[:type] || Object
end