Class: Features::Feature
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Features::Feature
- Defined in:
- lib/features/feature.rb
Overview
If you want to add a feature, simply define a suitable symbol to Feature::LIST. If for example you want to define the “wiffle feature”, add :wiffle and you can subsequenctly do:
account.features.wiffle?
account.features.wiffle.destroy
account.features.wiffle.create
Constant Summary collapse
- LIST =
[]
Class Method Summary collapse
- .class_to_sym(klass) ⇒ Object
- .dependant_features ⇒ Object
- .protected? ⇒ Boolean
- .required_features ⇒ Object
- .sym_to_class(sym) ⇒ Object
- .sym_to_name(sym) ⇒ Object
- .to_sym ⇒ Object
Instance Method Summary collapse
- #available? ⇒ Boolean
- #create ⇒ Object
- #dependant_features ⇒ Object
- #matches?(sym) ⇒ Boolean
- #protected? ⇒ Boolean
- #required_features ⇒ Object
- #to_sym ⇒ Object
- #unique_type ⇒ Object
Class Method Details
.class_to_sym(klass) ⇒ Object
52 53 54 |
# File 'lib/features/feature.rb', line 52 def self.class_to_sym(klass) klass.name.tableize.tableize[0..-10].to_sym end |
.dependant_features ⇒ Object
78 79 80 |
# File 'lib/features/feature.rb', line 78 def self.dependant_features @dependant_features ||= [] end |
.protected? ⇒ Boolean
64 65 66 |
# File 'lib/features/feature.rb', line 64 def self.protected? @protect || false end |
.required_features ⇒ Object
71 72 73 |
# File 'lib/features/feature.rb', line 71 def self.required_features @required_features ||= [] end |
.sym_to_class(sym) ⇒ Object
60 61 62 |
# File 'lib/features/feature.rb', line 60 def self.sym_to_class(sym) sym_to_name(sym).constantize end |
.sym_to_name(sym) ⇒ Object
56 57 58 |
# File 'lib/features/feature.rb', line 56 def self.sym_to_name(sym) "#{sym.to_s.camelize}Feature" end |
.to_sym ⇒ Object
48 49 50 |
# File 'lib/features/feature.rb', line 48 def self.to_sym @sym ||= Feature.class_to_sym(self) end |
Instance Method Details
#available? ⇒ Boolean
28 29 30 |
# File 'lib/features/feature.rb', line 28 def available? feature_owner_instance.features.available?(to_sym) end |
#create ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/features/feature.rb', line 32 def create if new_record? raise RequirementsError.new unless available? super end self end |
#dependant_features ⇒ Object
81 82 83 |
# File 'lib/features/feature.rb', line 81 def dependant_features self.class.dependant_features end |
#matches?(sym) ⇒ Boolean
40 41 42 |
# File 'lib/features/feature.rb', line 40 def matches?(sym) to_sym == sym.to_sym end |
#protected? ⇒ Boolean
67 68 69 |
# File 'lib/features/feature.rb', line 67 def protected? self.class.protected? end |
#required_features ⇒ Object
74 75 76 |
# File 'lib/features/feature.rb', line 74 def required_features self.class.required_features end |
#to_sym ⇒ Object
44 45 46 |
# File 'lib/features/feature.rb', line 44 def to_sym @sym ||= Feature.class_to_sym(self.class) end |
#unique_type ⇒ Object
24 25 26 |
# File 'lib/features/feature.rb', line 24 def unique_type errors.add(:feature, :taken) if self.class.exists?(self.class.feature_owner_key => send(self.class.feature_owner_key)) end |