Module: Evolvable::Community::ClassMethods
- Defined in:
- lib/evolvable/community.rb
Instance Attribute Summary collapse
-
#community_config ⇒ Object
Returns the value of attribute community_config.
Instance Method Summary collapse
-
#add_population(name, population) ⇒ Object
Adds a population to the community.
-
#evolvable_community(community_config) ⇒ void
Configures a class as an evolvable community with specified populations.
-
#initialize_community ⇒ Object
Initializes a new community instance This method exists as a hook for classes that include this module to override and customize the community instantiation process.
- #load_community(data) ⇒ Object
-
#new_community ⇒ Object
Creates a new instance of the community.
-
#populations_by_name ⇒ Hash
Returns a hash of population instances by name.
Instance Attribute Details
#community_config ⇒ Object
Returns the value of attribute community_config.
77 78 79 |
# File 'lib/evolvable/community.rb', line 77 def community_config @community_config end |
Instance Method Details
#add_population(name, population) ⇒ Object
Adds a population to the community
105 106 107 |
# File 'lib/evolvable/community.rb', line 105 def add_population(name, population) populations_by_name[name.to_sym] = population end |
#evolvable_community(community_config) ⇒ void
This method returns an undefined value.
Configures a class as an evolvable community with specified populations
82 83 84 85 86 87 88 89 90 |
# File 'lib/evolvable/community.rb', line 82 def evolvable_community(community_config) self.community_config = community_config community_config.each_key do |name| method_name = name.to_sym define_method(method_name) do instance_variable_get("@#{method_name}") || instance_variable_set("@#{method_name}", find_or_new_evolvable(method_name)) end end end |
#initialize_community ⇒ Object
Initializes a new community instance This method exists as a hook for classes that include this module to override and customize the community instantiation process.
69 70 71 |
# File 'lib/evolvable/community.rb', line 69 def initialize_community new end |
#load_community(data) ⇒ Object
73 74 75 |
# File 'lib/evolvable/community.rb', line 73 def load_community(data) Serializer.load(data) end |
#new_community ⇒ Object
Creates a new instance of the community
61 62 63 |
# File 'lib/evolvable/community.rb', line 61 def new_community initialize_community end |
#populations_by_name ⇒ Hash
Returns a hash of population instances by name
94 95 96 97 98 99 |
# File 'lib/evolvable/community.rb', line 94 def populations_by_name @populations_by_name ||= community_config.inject({}) do |hash, (name, population_klass)| hash[name.to_sym] = population_klass.new_population(size: 0) hash end end |