class Ciesta::FieldList

List of object fields

@attr_reader [Hash<Symbol, Ciesta::Field>] list Field list

Attributes

list[R]

Public Class Methods

define(definitions) click to toggle source

Define a new field list

@api private @param [Hash] definitions Hash of fields definitions

@return Ciesta::FieldList

# File lib/ciesta/field_list.rb, line 13
def self.define(definitions)
  definitions.each_with_object(new) do |(name, options), list|
    list << Ciesta::Field.new(name, options)
  end
end
new() click to toggle source

Constructor

# File lib/ciesta/field_list.rb, line 20
def initialize
  @list = {}
end

Public Instance Methods

<<(field) click to toggle source

Adds new field to list

@param [Ciesta::Field] field New field @api private

# File lib/ciesta/field_list.rb, line 28
def <<(field)
  list[field.name] = field
end
[](name) click to toggle source

Getting field by name

@api private @param [Symbol] name Field name

@return [Ciesta::Field]

# File lib/ciesta/field_list.rb, line 38
def [](name)
  list[name.to_sym].value
end
[]=(name, value) click to toggle source

Setting field value

@api private @param [Symbol] name Field name @param [any] value Field value

# File lib/ciesta/field_list.rb, line 47
def []=(name, value)
  list[name.to_sym].value = value
end
assign(attributes) click to toggle source

Mass assign values to fields

@param [Hash<Symbol, any>] attributes Attributes

@return [Boolean]

# File lib/ciesta/field_list.rb, line 68
def assign(attributes)
  attributes = attributes.keep_if { |key| keys.include?(key) }
  begin
    assign!(attributes)
  rescue Ciesta::FieldNotDefined # rubocop:disable Lint/HandleExceptions
    # do nothing
  end
end
assign!(attributes) click to toggle source

Mass assign values to fields

@param [Hash<Symbol, any>] attributes Attributes

@raise Ciesta::FieldNotDefined @return [Boolean]

# File lib/ciesta/field_list.rb, line 57
def assign!(attributes)
  attributes.each { |name, value| self[name] = value }
rescue NoMethodError => e
  raise Ciesta::FieldNotDefined, "Field #{e.name} is not specified"
end
attributes() click to toggle source

Getting all field values

@api private @return [Hash<Symbol, any>]

# File lib/ciesta/field_list.rb, line 89
def attributes
  list.values.map { |field| [field.name, field.value] }.to_h
end
clear!() click to toggle source

Clear all fields

@api private

# File lib/ciesta/field_list.rb, line 96
def clear!
  list.each_value(&:clear!)
end
keys() click to toggle source

Getting all field names

@api private @return [Array<Symbol>]

# File lib/ciesta/field_list.rb, line 81
def keys
  list.keys
end