Module: Moon::DataModel::Fields::InstanceMethods

Includes:
Enumerable, Serializable::Properties::InstanceMethods
Included in:
Moon::DataModel::Fields
Defined in:
lib/data_model/fields/instance_methods.rb

Overview

Instance methods for models

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Gets a field's value with the provided key This is used to interface with Hash and OpenStruct

Parameters:

  • key (Symbol)

Returns:

  • (Object)

256
257
258
# File 'lib/data_model/fields/instance_methods.rb', line 256

def [](key)
  field_get key
end

#[]=(key, value) ⇒ Object

Sets a field with the provided key and value This is used to interface with Hash and OpenStruct

Parameters:

  • key (Symbol)
  • value (Object)

265
266
267
# File 'lib/data_model/fields/instance_methods.rb', line 265

def []=(key, value)
  field_set key, value
end

#assoc(key) ⇒ Array[Symbol, Object]

Create and return an assoc pair, the first element is the field name and the second is the value of the field.

Returns:

  • (Array[Symbol, Object])

    name, value


121
122
123
# File 'lib/data_model/fields/instance_methods.rb', line 121

def assoc(key)
  [key, field_get(key)]
end

#each_field {|key, field| ... } ⇒ Object

Yields each Moon::DataModel::Field this model has

Examples:

each_field do |key, field|
end

Yield Parameters:

  • key (Symbol)
  • field (Field)

133
134
135
136
# File 'lib/data_model/fields/instance_methods.rb', line 133

def each_field(&block)
  return to_enum :each_field unless block_given?
  self.class.each_field.each(&block)
end

#each_field_name {|key| ... } ⇒ Object Also known as: each_key

Examples:

each_field_name do |key|
end

Yield Parameters:

  • key (Symbol)

145
146
147
148
149
150
# File 'lib/data_model/fields/instance_methods.rb', line 145

def each_field_name
  return to_enum :each_field_name unless block_given?
  each_field do |k, _|
    yield k
  end
end

#each_field_with_value {|key, field, value| ... } ⇒ Object

Yields each Moon::DataModel::Field, along with its current value on the Model.

Examples:

each_field_with_value do |key, field, value|
end

Yield Parameters:

  • key (Symbol)
  • field (Field)
  • value (Object)

162
163
164
165
166
167
# File 'lib/data_model/fields/instance_methods.rb', line 162

def each_field_with_value
  return to_enum :each_field_with_value unless block_given?
  each_field do |k, field|
    yield k, field, field_get(k)
  end
end

#each_pair {|key, value| ... } ⇒ Object Also known as: each

Yields each Moon::DataModel::Field#name and value

Examples:

each_pair do |key, value|
end

Yield Parameters:

  • key (Symbol)
  • value (Objecy)

177
178
179
180
181
182
# File 'lib/data_model/fields/instance_methods.rb', line 177

def each_pair
  return to_enum :each_pair unless block_given?
  each_field_with_value do |key, _, value|
    yield key, value
  end
end

#each_value {|value| ... } ⇒ Object

Yields each Field's value

Examples:

each_value do |value|
end

Yield Parameters:

  • value (Object)

191
192
193
194
195
196
# File 'lib/data_model/fields/instance_methods.rb', line 191

def each_value
  return to_enum :each_value unless block_given?
  each_field_with_value do |_, _, value|
    yield value
  end
end

#field_get(key) ⇒ Object

Retrieves a field value by key

Parameters:

  • key (Symbol)

Returns:

  • (Object)

91
92
93
94
# File 'lib/data_model/fields/instance_methods.rb', line 91

def field_get(key)
  self.class.fetch_field(key.to_sym) # ensure that the field exists
  send key
end

#field_set(key, value) ⇒ void Also known as: store

This method returns an undefined value.

Sets a field value by key, the value will be coerced

Parameters:

  • key (Symbol)
  • value (Object)

68
69
70
71
# File 'lib/data_model/fields/instance_methods.rb', line 68

def field_set(key, value)
  field = self.class.fetch_field(key.to_sym)
  send "#{key}=", field.coerce(value)
end

#field_set!(key, value) ⇒ void

This method returns an undefined value.

Hard sets a field value by key, the value will bypass validations and coercion. WARNING: Only use this method if you know what you're doing.

Parameters:

  • key (Symbol)
  • value (Object)

83
84
85
# File 'lib/data_model/fields/instance_methods.rb', line 83

def field_set!(key, value)
  send "_#{key}_set", value
end

#fields=(opts) ⇒ Object

Set internal attributes using the hash key~value pairs. These attributes are subject to validation, use #update_fields! instead if validation needs to be bypassed.

Parameters:

  • opts (Hash<Symbol, Object>)

220
221
222
# File 'lib/data_model/fields/instance_methods.rb', line 220

def fields=(opts)
  update_fields(opts)
end

#fields_hashHash<Symbol, Object> Also known as: fields

Returns a Hash of all the fields key and value

Returns:

  • (Hash<Symbol, Object>)

212
213
214
# File 'lib/data_model/fields/instance_methods.rb', line 212

def fields_hash
  each_pair.to_h
end

#initialize_fields(options = {}) ⇒ void

This method returns an undefined value.

Called by the constructor to setup initial fields values

Parameters:

  • options (Hash<Symbol, Object>) (defaults to: {})

56
57
58
59
60
61
# File 'lib/data_model/fields/instance_methods.rb', line 56

def initialize_fields(options = {})
  pre_initialize_fields
  update_fields(options) unless options.empty?
  initialize_fields_default(options.keys)
  post_initialize_fields
end

#invalid?Boolean

Checks if the model is invalid

Returns:

  • (Boolean)

237
238
239
# File 'lib/data_model/fields/instance_methods.rb', line 237

def invalid?
  !valid?
end

#post_initialize_fieldsObject

Callback after fields are initialized.


49
50
# File 'lib/data_model/fields/instance_methods.rb', line 49

def post_initialize_fields
end

#pre_initialize_fieldsObject

Callback before fields are initialized by given options and defaults.


45
46
# File 'lib/data_model/fields/instance_methods.rb', line 45

def pre_initialize_fields
end

#reset_field(key) ⇒ void

This method returns an undefined value.

Initializes a field to its default value

Parameters:

  • key (Symbol)

    name of the field to initialize


18
19
20
21
# File 'lib/data_model/fields/instance_methods.rb', line 18

def reset_field(key)
  field = self.class.fetch_field(key)
  field_set key, field.make_default(self)
end

#reset_fieldsvoid

This method returns an undefined value.

Initializes all available fields for the model with their default value


27
28
29
30
31
# File 'lib/data_model/fields/instance_methods.rb', line 27

def reset_fields
  each_field_name do |key|
    reset_field(key)
  end
end

#update_fields(opts) ⇒ Object

Set internal attributes using the hash key~value pairs. These attributes are subject to validation, use #update_fields! instead if validation needs to be bypassed.

Parameters:

  • opts (Hash<Symbol, Object>)

101
102
103
104
# File 'lib/data_model/fields/instance_methods.rb', line 101

def update_fields(opts)
  opts.each_pair { |k, v| field_set k, v }
  self
end

#update_fields!(opts) ⇒ Object

Sets internal attributes using the Hash key~value pairs. These attributes bypass validation, use #update_fields instead if validation is needed. Only use this method if you know what you're doing.

Parameters:

  • opts (Hash<Symbol, Object>)

112
113
114
115
# File 'lib/data_model/fields/instance_methods.rb', line 112

def update_fields!(opts)
  opts.each_pair { |k, v| field_set! k, v }
  self
end

#valid?Boolean

Checks if fields are valid, returns false on the first failure.

Returns:

  • (Boolean)

227
228
229
230
231
232
# File 'lib/data_model/fields/instance_methods.rb', line 227

def valid?
  each_field_with_value do |_, field, value|
    return false unless field.valid?(value)
  end
  true
end

#validateself

Runs the validation for each field on the model.

Returns:

  • (self)

244
245
246
247
248
249
# File 'lib/data_model/fields/instance_methods.rb', line 244

def validate
  each_field_with_value do |key, field, value|
    field.validate(value)
  end
  self
end

#validate_fields?Boolean

Should fields be validated? see also Moon::DataModel::Field#validate

Returns:

  • (Boolean)

    by default, this method always returns true


205
206
207
# File 'lib/data_model/fields/instance_methods.rb', line 205

def validate_fields?
  true
end