Class: Ciesta::FieldList

Inherits:
Object
  • Object
show all
Defined in:
lib/ciesta/field_list.rb

Overview

List of object fields

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldList

Constructor



20
21
22
# File 'lib/ciesta/field_list.rb', line 20

def initialize
  @list = {}
end

Class Method Details

.define(definitions) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define a new field list

Parameters:

  • definitions (Hash)

    Hash of fields definitions

Returns:

  • Ciesta::FieldList



13
14
15
16
17
# 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

Instance Method Details

#<<(field) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds new field to list

Parameters:



28
29
30
# File 'lib/ciesta/field_list.rb', line 28

def <<(field)
  list[field.name] = field
end

#[](name) ⇒ Ciesta::Field

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Getting field by name

Parameters:

  • name (Symbol)

    Field name

Returns:



38
39
40
# File 'lib/ciesta/field_list.rb', line 38

def [](name)
  list[name.to_sym].value
end

#[]=(name, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Setting field value

Parameters:

  • name (Symbol)

    Field name

  • value (any)

    Field value



47
48
49
# File 'lib/ciesta/field_list.rb', line 47

def []=(name, value)
  list[name.to_sym].value = value
end

#assign(attributes) ⇒ Boolean

Mass assign values to fields

Parameters:

  • attributes (Hash<Symbol, any>)

    Attributes

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# 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) ⇒ Boolean

Mass assign values to fields

Parameters:

  • attributes (Hash<Symbol, any>)

    Attributes

Returns:

  • (Boolean)

Raises:

  • Ciesta::FieldNotDefined



57
58
59
60
61
# 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

#attributesHash<Symbol, any>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Getting all field values

Returns:

  • (Hash<Symbol, any>)


89
90
91
# File 'lib/ciesta/field_list.rb', line 89

def attributes
  list.values.map { |field| [field.name, field.value] }.to_h
end

#clear!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear all fields



96
97
98
# File 'lib/ciesta/field_list.rb', line 96

def clear!
  list.each_value(&:clear!)
end

#keysArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Getting all field names

Returns:

  • (Array<Symbol>)


81
82
83
# File 'lib/ciesta/field_list.rb', line 81

def keys
  list.keys
end