Module: Algebrick::FieldMethodReaders

Included in:
ParametrizedType, ProductVariant
Defined in:
lib/algebrick/field_method_readers.rb

Instance Method Summary collapse

Instance Method Details

#add_all_field_method_readersObject



38
39
40
41
# File 'lib/algebrick/field_method_readers.rb', line 38

def add_all_field_method_readers
  add_field_method_readers *field_names
  self
end

#add_field_method_reader(field) ⇒ Object

Raises:

  • (TypeError)


25
26
27
28
29
30
31
# File 'lib/algebrick/field_method_readers.rb', line 25

def add_field_method_reader(field)
  raise TypeError, 'no field names' unless field_names?
  raise ArgumentError, "no field name #{field}" unless field_names.include? field
  raise ArgumentError, "method #{field} already defined" if instance_methods.include? field
  define_method(field) { self[field] }
  self
end

#add_field_method_readers(*fields) ⇒ Object



33
34
35
36
# File 'lib/algebrick/field_method_readers.rb', line 33

def add_field_method_readers(*fields)
  fields.each { |f| add_field_method_reader f }
  self
end

#field_namesObject



17
18
19
# File 'lib/algebrick/field_method_readers.rb', line 17

def field_names
  @field_names or raise TypeError, "field names not defined on #{self}"
end

#field_names?Boolean

Returns:



21
22
23
# File 'lib/algebrick/field_method_readers.rb', line 21

def field_names?
  !!@field_names
end