Class: EasyType::Group
- Inherits:
-
Object
- Object
- EasyType::Group
- Defined in:
- lib/easy_type/group.rb
Overview
A [Group] is a definition of a set of [Parameter] and/or [Property] classes. When you add a [Property] or a [Parameter] to a [Group], you specify that whenever one of the properties is changed, all the ‘on_modify` methods of all parameters and properties are called.
This is a mechanism to ensure an ‘on_update` or `on_create` always is syntacticaly correct and has all the information needed.
Instance Method Summary collapse
-
#add(group_name, parameter_or_property) ⇒ Group
Add a parameter or a propert to a group.
-
#contents_for(group_name) ⇒ Array
Get all parameters and properties from the specified group name.
-
#group_for(parameter_or_property) ⇒ Symbol
returns the group name for a given parameter or property.
-
#include?(group_name) ⇒ Boolean
Returns true if the group exists.
-
#include_property?(parameter_or_property) ⇒ Boolean
Returns true if the paremeter or property is included in any existing group.
-
#initialize ⇒ Group
constructor
A new instance of Group.
Constructor Details
#initialize ⇒ Group
Returns a new instance of Group.
18 19 20 |
# File 'lib/easy_type/group.rb', line 18 def initialize @content = {} end |
Instance Method Details
#add(group_name, parameter_or_property) ⇒ Group
Add a parameter or a propert to a group. The may or may not exists. If it doesn’t exist, it will be created. The name of a group is just for identification purpose’s. It doesn’t have any other meaning.
47 48 49 50 51 |
# File 'lib/easy_type/group.rb', line 47 def add(group_name, parameter_or_property) group = ensure_group(group_name) group << parameter_or_property group end |
#contents_for(group_name) ⇒ Array
Get all parameters and properties from the specified group name. If the group doesn’t exist raise an exception
30 31 32 33 34 |
# File 'lib/easy_type/group.rb', line 30 def contents_for(group_name) @content.fetch(group_name) do fail "easy_type: No group defined with name #{group_name}" end end |
#group_for(parameter_or_property) ⇒ Symbol
returns the group name for a given parameter or property. If the group doesn’t exist, it will raise an error
62 63 64 65 66 67 |
# File 'lib/easy_type/group.rb', line 62 def group_for(parameter_or_property) @content.each_pair do |key, value| return key if value.include?(parameter_or_property) end fail "easy_type: #{parameter_or_property} not found in any group" end |
#include?(group_name) ⇒ Boolean
Returns true if the group exists
76 77 78 |
# File 'lib/easy_type/group.rb', line 76 def include?(group_name) @content.keys.include?(group_name) end |
#include_property?(parameter_or_property) ⇒ Boolean
Returns true if the paremeter or property is included in any existing group
88 89 90 |
# File 'lib/easy_type/group.rb', line 88 def include_property?(parameter_or_property) @content.values.flatten.include?(parameter_or_property) end |