Class: FeduxOrgStdlib::Models::BaseModel
- Inherits:
-
Object
- Object
- FeduxOrgStdlib::Models::BaseModel
- Includes:
- Comparable
- Defined in:
- lib/fedux_org_stdlib/models/base_model.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
-
.all_names_as_string(connector = ", ") ⇒ Object
return all names as string.
- .clear ⇒ Object
- .create(*args, &block) ⇒ Object
- .delete(val) ⇒ Object
-
.enable(name) ⇒ Object
enables a specific instance.
-
.find(criteria = {}) ⇒ Object
finds a single instance.
-
.find_all(criteria = {}) ⇒ Object
finds all instances.
- .first ⇒ Object
- .inherited(base) ⇒ Object
- .last ⇒ Object
-
.register(element) ⇒ Object
attr_accessor :instances.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#enable ⇒ Object
enable action.
-
#enabled?(val = true) ⇒ Boolean
check if action is enabled.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name) ⇒ BaseModel
constructor
A new instance of BaseModel.
- #name?(name) ⇒ Boolean
Constructor Details
#initialize(name) ⇒ BaseModel
Returns a new instance of BaseModel.
20 21 22 23 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 20 def initialize(name) @name = name.to_sym @enabled = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 14 def name @name end |
Class Method Details
.all ⇒ Object
80 81 82 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 80 def all @instances.to_a end |
.all_names_as_string(connector = ", ") ⇒ Object
return all names as string
85 86 87 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 85 def all_names_as_string(connector=", ") find_all(enabled: true).map(&:name).sort.join(connector) end |
.clear ⇒ Object
76 77 78 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 76 def clear @instances = Set.new end |
.create(*args, &block) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 60 def create( *args, &block ) if block_given? register( new( *args, &block ) ) else register( new( *args ) ) end end |
.delete(val) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 68 def delete( val ) element = self.find( val.to_s.to_sym ) raise FeduxOrgStdlib::Exceptions::InstanceNotFound unless element @instances.delete element element end |
.enable(name) ⇒ Object
enables a specific instance
98 99 100 101 102 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 98 def enable(name) find(name: name).enable rescue NoMethodError => e raise FeduxOrgStdlib::Models::Exceptions::InvalidSearchCriteria, "Sorry I'm not able to enable \"#{name}\". You need to create an instance with name \"#{name}\" first." end |
.find(criteria = {}) ⇒ Object
finds a single instance
105 106 107 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 105 def find(criteria = {}) find_all(criteria).first end |
.find_all(criteria = {}) ⇒ Object
finds all instances
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 110 def find_all(criteria = {}) FeduxOrgStdlib.logger.debug(self) { "Criteria for search: #{ criteria }" } criteria = { name: criteria.to_sym } if criteria.kind_of? Symbol or criteria.kind_of? String FeduxOrgStdlib.logger.debug(self) { "Instances to be searched for: #{ @instances.map { |i| "#{i.name} (#{i.class})" }.join(", ") }" } @instances.find_all do |i| criteria.all? do |c,v| FeduxOrgStdlib.logger.debug(self) { "Check method for search: #{ c }" } i.send( "#{c}?".to_sym , v ) end end rescue NameError => e raise FeduxOrgStdlib::Models::Exceptions::InvalidSearchCriteria, e. end |
.first ⇒ Object
89 90 91 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 89 def first @instances.to_a.first end |
.inherited(base) ⇒ Object
16 17 18 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 16 def self.inherited(base) base.instance_variable_set(:@instances, Set.new) end |
.last ⇒ Object
93 94 95 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 93 def last @instances.to_a.last end |
.register(element) ⇒ Object
attr_accessor :instances
54 55 56 57 58 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 54 def register(element) @instances << element element end |
Instance Method Details
#<=>(other) ⇒ Object
39 40 41 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 39 def <=>(other) name <=> other.name end |
#enable ⇒ Object
enable action
26 27 28 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 26 def enable @enabled = true end |
#enabled?(val = true) ⇒ Boolean
check if action is enabled
31 32 33 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 31 def enabled?(val=true) @enabled == val end |
#eql?(other) ⇒ Boolean
43 44 45 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 43 def eql?(other) name == other.name end |
#hash ⇒ Object
47 48 49 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 47 def hash name.to_s.hash end |
#name?(name) ⇒ Boolean
35 36 37 |
# File 'lib/fedux_org_stdlib/models/base_model.rb', line 35 def name?(name) @name == name end |