module DigitalOpera::Document
Public Instance Methods
Add the defaults to the model. This breaks them up between ones that are procs and ones that are not.
@example Add to the defaults.
Model.add_defaults(field)
@param [ Field ] field The field to add for.
@since 2.4.0
# File lib/digital_opera/document.rb, line 116 def add_defaults(field) default, name = field.default_val, field.name.to_s remove_defaults(name) unless default.nil? if field.pre_processed? pre_processed_defaults.push(name) else post_processed_defaults.push(name) end end end
Define a field attribute for the Document
.
@example Set the field.
Person.add_field(:name, :default => "Test")
@param [ Symbol ] name The name of the field. @param [ Hash ] options The hash of options.
# File lib/digital_opera/document.rb, line 135 def add_field(name, options = {}) # aliased = options[:as] # aliased_fields[aliased.to_s] = name if aliased field = field_for(name, options) fields[name] = field # add_defaults(field) create_accessors(name, name, options) # process_options(field) # create_dirty_methods(name, name) # create_dirty_methods(name, aliased) if aliased field end
# File lib/digital_opera/document.rb, line 15 def attribute_names self.class.fields.keys.reject{ |k| !respond_to?(k) } end
Create the getter method for the provided field.
@example Create the getter.
Model.create_field_getter("name", "name", field)
@param [ String ] name The name of the attribute. @param [ String ] meth The name of the method. @param [ Field ] field The field.
@since 2.4.0
# File lib/digital_opera/document.rb, line 72 def create_field_getter(name, meth, field) generated_methods.module_eval do define_method("#{ meth }") do value = instance_variable_get "@#{ name }" value.nil? ? field.default_val : value end end end
Create the setter method for the provided field.
@example Create the setter.
Model.create_field_setter("name", "name")
@param [ String ] name The name of the attribute. @param [ String ] meth The name of the method. @param [ Field ] field The field.
@since 2.4.0
# File lib/digital_opera/document.rb, line 99 def create_field_setter(name, meth, field) generated_methods.module_eval do define_method("#{ meth }=") do |value| instance_variable_set "@#{ name }", value end end end
# File lib/digital_opera/document.rb, line 161 def field_for(name, options) opts = options.merge(klass: self) Fields::Standard.new(name, opts) end
# File lib/digital_opera/document.rb, line 81 def generated_methods @generated_methods ||= begin mod = Module.new include(mod) mod end end
Remove the default keys for the provided name.
@example Remove the default keys.
Model.remove_defaults(name)
@param [ String ] name The field name.
@since 2.4.0
# File lib/digital_opera/document.rb, line 156 def remove_defaults(name) pre_processed_defaults.delete_one(name) post_processed_defaults.delete_one(name) end