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

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

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

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

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

Create a new component with the specified values.

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

Public Instance Methods

fields()

The Hash of fields implemented by the component

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

Protected Instance Methods

fields_description()

Return a description of the fields this component has.

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

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

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