Module: Moon::DataModel::Fields::Modelling

Extended by:
Prototype
Includes:
Serializable::Properties::ClassMethods
Included in:
ClassMethods
Defined in:
lib/data_model/fields/modelling.rb

Overview

Methods for setting up, and finding fields on a Model class.

Instance Method Summary collapse

Instance Method Details

#add_field(field) ⇒ Symbol

Registers and creates accessors for a given field

Parameters:

Returns:

  • (Symbol)

    field name

  • (Symbol)

138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/data_model/fields/modelling.rb', line 138

def add_field(field)
  fields[field.name] = field

  # first setup the Serializable property, this also creates the
  # initial attr for us
  property_accessor field.name
  # next we'll need to overwrite the writer created by property_accessor,
  # with our field validation one.
  define_field_writer field, field.name

  field.name
end

#array(sym, options) ⇒ Symbol

Defines a new Array field, is a shorthand for field type: [Type]

Parameters:

  • sym (Symbol)
  • options (Hash<Symbol, Object>)

Options Hash (options):

  • :size (Integer)

    default: 0

  • :default (Proc, Object)
  • :type (Module)

    content type of the Array

Returns:

  • (Symbol)

179
180
181
182
183
184
# File 'lib/data_model/fields/modelling.rb', line 179

def array(sym, options)
  size = options.fetch(:size, 0)
  default = options.fetch(:default) { ->(_, _) { Array.new(size) } }
  type = Array[options.fetch(:type)]
  field sym, options.merge(type: type, default: default)
end

#define_field(name, options) ⇒ Symbol

Defines a new, without option adjustments

Parameters:

  • name (Symbol)
  • options (Hash)

Returns:

  • (Symbol)

158
159
160
# File 'lib/data_model/fields/modelling.rb', line 158

def define_field(name, options)
  add_field Field.new(options.merge(name: name.to_sym))
end

#dict(sym, options) ⇒ Symbol

Defines a new Hash field, is a shorthand for field type: Type=>Type

Parameters:

  • sym (Symbol)
  • options (Hash<Symbol, Object>)

Options Hash (options):

  • :default (Proc, Object)
  • :key (Module)

    key type

  • :value (Module)

    value type

Returns:

  • (Symbol)

194
195
196
197
198
# File 'lib/data_model/fields/modelling.rb', line 194

def dict(sym, options)
  default = options.fetch(:default) { ->(_, _) { Hash.new } }
  type = Hash[options.fetch(:key) => options.fetch(:value)]
  field sym, options.merge(type: type, default: default)
end

#fetch_field(expected_key = nil, &block) ⇒ Field

Locates a field by name, if teh field is not found a FieldNotFound is raised. (see #find_field)

Returns:


51
52
53
54
55
# File 'lib/data_model/fields/modelling.rb', line 51

def fetch_field(expected_key = nil, &block)
  find_field(expected_key, &block) ||
    (raise FieldNotFound,
      "no such field `#{expected_key}` for #{self}.")
end

#field(name, options = {}) ⇒ Symbol

Defines a new field with option adjustments

Parameters:

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

Returns:

  • (Symbol)

167
168
169
# File 'lib/data_model/fields/modelling.rb', line 167

def field(name, options = {})
  define_field name, Fields.adjust_field_options(self, options)
end

#field_setting(key) ⇒ Object #field_setting(key, value) ⇒ void #field_setting(options) ⇒ void

Field settings are common parameters amongst fields, it can be used to setup mulitple fields with the same parameters. Anyway setting/options that can be used on a field line can be used as a setting.

A block can be passed in to temporarily use the field_settings within the block's context.

Examples:

field_setting type: String do
  # all fields declared here will have the type String
  field :id
  field :name
  field :secret
end

Overloads:

  • #field_setting(key) ⇒ Object

    Retrives field_setting value by key

    Examples:

    Basic usage

    field_setting :default

    Parameters:

    • key (Symbol)

    Returns:

    • (Object)

      value at key

  • #field_setting(key, value) ⇒ void

    This method returns an undefined value.

    Sets a field_setting value by key

    Examples:

    Basic usage

    field_setting :default, proc { |type| type.model.new }

    Parameters:

    • key (Symbol)
    • value (Object)
  • #field_setting(options) ⇒ void

    This method returns an undefined value.

    Merges the options into the field settings

    Examples:

    Basic usage

    field_setting type: String

    Parameters:

    • options (Hash)

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/data_model/fields/modelling.rb', line 96

def field_setting(obj, *args)
  # allows you to temporarily apply the field_settings to the block.
  if block_given?
    org = field_settings.dup
    field_setting(obj, *args)
    yield self
    field_settings.clear
    field_setting org
  else
    if Hash === obj
      field_settings.merge!(obj)
    else
      if args.size > 0
        field_settings[obj] = args.first
      else
        field_settings[obj]
      end
    end
  end
end

#finalizevoid

This method returns an undefined value.

Finalizes incomplete fields, see Moon::DataModel::Field#finalize


21
22
23
24
25
# File 'lib/data_model/fields/modelling.rb', line 21

def finalize
  fields.each do |_, field|
    field.finalize
  end
end

#find_field(expected_key = nil) {|key| ... } ⇒ Field?

Locates a field by name

Parameters:

  • expected_key (Symbol) (defaults to: nil)

    expected field name

Yield Parameters:

  • key (Symbol)

    name

Returns:

  • (Field, nil)

    returns the field for the corresponding key, or nil if the key was not found.


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/data_model/fields/modelling.rb', line 33

def find_field(expected_key = nil, &block)
  if block_given?
    each_field do |key, field|
      return field if block.call(key, field)
    end
  else
    each_field do |key, field|
      return field if expected_key == key
    end
  end
  nil
end