Class: JsonRecord::Schema
- Inherits:
-
Object
- Object
- JsonRecord::Schema
- Defined in:
- lib/json_record/schema.rb
Overview
Definition of a document schema. Defining a schema will define accessor methods for each field and potentially some validators.
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#json_field_name ⇒ Object
readonly
Returns the value of attribute json_field_name.
Instance Method Summary collapse
-
#initialize(klass, json_field_name) ⇒ Schema
constructor
Create a schema on the class for a particular field.
-
#key(name, *args) ⇒ Object
Define a single valued field in the schema.
-
#many(name, *args) ⇒ Object
Define a multi valued field in the schema.
Constructor Details
#initialize(klass, json_field_name) ⇒ Schema
Create a schema on the class for a particular field.
8 9 10 11 12 |
# File 'lib/json_record/schema.rb', line 8 def initialize (klass, json_field_name) @json_field_name = json_field_name @klass = klass @fields = {} end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/json_record/schema.rb', line 5 def fields @fields end |
#json_field_name ⇒ Object (readonly)
Returns the value of attribute json_field_name.
5 6 7 |
# File 'lib/json_record/schema.rb', line 5 def json_field_name @json_field_name end |
Instance Method Details
#key(name, *args) ⇒ Object
Define a single valued field in the schema. The first argument is the field name. This must be unique for the class accross all attributes.
The optional second argument can be used to specify the class of the field values. It will default to String if not specified. Valid types are String, Integer, Float, Date, Time, DateTime, Boolean, Array, Hash, or any class that inherits from EmbeddedDocument.
The last argument can be a hash with any of these keys:
-
:default -
-
:required -
-
:length -
-
:format -
-
:in -
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/json_record/schema.rb', line 27 def key (name, *args) = args. name = name.to_s json_type = args.first || String raise ArgumentError.new("too many arguments (must be 1 or 2 plus an options hash)") if args.length > 1 field = FieldDefinition.new(name, :type => json_type, :default => [:default]) fields[name] = field add_json_validations(field, ) define_json_accessor(field, json_field_name) end |
#many(name, *args) ⇒ Object
Define a multi valued field in the schema. The first argument is the field name. This must be unique for the class accross all attributes.
The optional second argument should be the class of field values. This class must include EmbeddedDocument. If it is not specified, the class name will be guessed from the field name.
The last argument can be :unique => field_name which is used to indicate that the values in the field must have unique values in the indicated field name.
The value of the field will always be an EmbeddedDocumentArray and adding and removing values is as simple as appending them to the array. You can also call the build method on the array to keep the syntax the same as when dealing with ActiveRecord has_many associations.
52 53 54 55 56 57 58 59 60 |
# File 'lib/json_record/schema.rb', line 52 def many (name, *args) name = name.to_s = args. type = args.first || name.singularize.classify.constantize field = FieldDefinition.new(name, :type => type, :multivalued => true) fields[name] = field add_json_validations(field, ) define_many_json_accessor(field, json_field_name) end |