Class: Features::ActiveRecordExtension::FeatureTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/features/active_record_extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner_class) ⇒ FeatureTreeBuilder

Returns a new instance of FeatureTreeBuilder.



87
88
89
90
# File 'lib/features/active_record_extension.rb', line 87

def initialize(owner_class)
  @owner_class = owner_class
  @definitions = Hash.new
end

Instance Method Details

#buildObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/features/active_record_extension.rb', line 107

def build
  resolve_dependencies
  #defines the feature classes
  @definitions.each do |name, options|
    new_feature = Object.const_set(Feature.sym_to_name(name), Class.new(Feature))
    new_feature.feature_owner = @owner_class
  end
  
  #sets the feature classes options
  @definitions.each do |name, options|
    new_feature = Feature.sym_to_class(name)
    new_feature.protect! if options[:protected]
    new_feature.required_features = options[:requires].map {|f| Feature.sym_to_class(f)}
    new_feature.dependant_features = options[:dependants].map {|f| Feature.sym_to_class(f)}

    Feature::LIST << name
  end
end

#feature(name, options = {}) ⇒ Object



92
93
94
95
96
97
# File 'lib/features/active_record_extension.rb', line 92

def feature(name, options = {})
  raise("Feature name '#{name}' is too long. Max 28 characters please...") if name.to_s.size > 28
  feature_options = options.reverse_merge({:requires => [], :dependants => [], :protected => false})
  feature_options[:requires] = [*feature_options[:requires]].uniq
  @definitions[name.to_sym] = feature_options
end

#resolve_dependenciesObject



99
100
101
102
103
104
105
# File 'lib/features/active_record_extension.rb', line 99

def resolve_dependencies
  @definitions.each do |name, options|
    options[:requires].each do |required_feature_name|
      @definitions[required_feature_name][:dependants] << name
    end
  end
end