Chione::

Component class

The Component (data) class

Attributes

entity_id RW

The ID of the entity the component belongs to

Public Class Methods

field( name, **options, &process_block )

Declare a field for the component named name, with a default value of default. If the optional process_block is provided, it will be called with the new value being assigned to the field before it is set, and the return value of it will be used instead.

   # File lib/chione/component.rb
43 def self::field( name, **options, &process_block )
44     options[ :processor ] = process_block
45     self.fields ||= {}
46     self.fields[ name ] = options
47 
48     # Add some class method
49     self.define_singleton_method( "processor_for_#{name}" ) do
50         return self.fields.dig( name, :processor )
51     end
52     self.define_singleton_method( "default_for_#{name}" ) do
53         default = self.fields.dig( name, :default )
54         return default.call( self ) if default.respond_to?( :call )
55         return Chione::DataUtilities.deep_copy( default )
56     end
57     self.define_singleton_method( "options_for_#{name}" ) do
58         return self.fields[ name ]
59     end
60 
61     # Add instance methods as a mixin so they can be overridden and super()ed to
62     mixin = self.make_field_mixin( name )
63     self.include( mixin )
64 
65 end
inherited( subclass )

Inheritance callback – add some default instance variable values to subclasses.

   # File lib/chione/component.rb
32 def self::inherited( subclass )
33     super
34 
35     subclass.fields ||= {}
36 end
make_field_mixin( name )

Make a mixin module with methods for the field with the specified name.

   # File lib/chione/component.rb
69 def self::make_field_mixin( name )
70     mixin = Module.new
71 
72     mixin.attr_reader( name )
73     mixin.define_method( "process_#{name}" ) do |value|
74         processor = self.class.send( "processor_for_#{name}" ) or return value
75         return processor.call( value )
76     end
77     mixin.define_method( "#{name}=" ) do |new_val|
78         new_val = self.send( "process_#{name}", new_val )
79         self.instance_variable_set( "@#{name}", new_val )
80     end
81 
82     return mixin
83 end
new( entity_id=nil, values={} )

Create a new component with the specified values.

    # File lib/chione/component.rb
 87 def initialize( entity_id=nil, values={} )
 88     if entity_id.is_a?( Hash )
 89         values = entity_id
 90         entity_id = nil
 91     end
 92 
 93     @entity_id = entity_id
 94 
 95     if self.class.fields
 96         self.class.fields.each_key do |name|
 97             val = values[ name ] || self.class.send( "default_for_#{name}" )
 98             self.public_send( "#{name}=", val )
 99         end
100     end
101 end

Public Instance Methods

fields()

The Hash of fields implemented by the component

   # File lib/chione/component.rb
27 singleton_attr_accessor :fields

Protected Instance Methods

fields_description()

Return a description of the fields this component has.

    # File lib/chione/component.rb
127 def fields_description
128     return self.class.fields.keys.collect do |name|
129         val = self.instance_variable_get( "@#{name}" )
130         "%s: %s" % [
131             name,
132             truncate_string( val.inspect, 20 )
133         ]
134     end.join( ' ' )
135 end
inspect_details()

Return the detailed part of the Component’s #inspect output.

    # File lib/chione/component.rb
118 def inspect_details
119     return "{%s} %s" % [
120         self.entity_id || "(unassigned)",
121         self.fields_description
122     ]
123 end