Module: Sprout::RubyFeature::ClassMethods

Defined in:
lib/sprout/ruby_feature.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) clear_entities!



84
85
86
# File 'lib/sprout/ruby_feature.rb', line 84

def clear_entities!
  @registered_entities = []
end

- (Object) entity_for(name_or_names, pkg_name, version_requirement) (protected)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sprout/ruby_feature.rb', line 113

def entity_for name_or_names, pkg_name, version_requirement
  # These commented blocks help immensely when debugging
  # loading and registration issues, please leave them here:
  #puts "+++++++++++++++++++++++++++"
  #puts ">> entity_for #{name_or_names} pkg_name: #{pkg_name} version: #{version_requirement}"
  #registered_entities.each do |entity|
    #puts ">> entity: #{entity.name} platform: #{entity.platform} pkg_name: #{entity.pkg_name} version: #{entity.pkg_version}"
  #end
  registered_entities.reverse.select do |entity|
      satisfies_name?(entity, name_or_names) && 
      satisfies_platform?(entity) &&
      satisfies_pkg_name?(entity, pkg_name) &&
      satisfies_version?(entity, version_requirement)
  end.first
end

- (Object) load(name_or_names, pkg_name = nil, version_requirement = nil)

Load a feature by name.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sprout/ruby_feature.rb', line 56

def load name_or_names, pkg_name=nil, version_requirement=nil
  entity = entity_for name_or_names, pkg_name, version_requirement
  if(entity.nil?)
    # Try to require the pkg_name:
    require_ruby_package pkg_name unless pkg_name.nil?
    # Update any entities that registered from our require:
    update_registered_entities
    # search for the requested entity:
    entity = entity_for name_or_names, pkg_name, version_requirement
  end

  if(entity.nil?)
    message = "The requested entity: (#{name_or_names}) with pkg_name: (#{pkg_name}) and version: "
    message << "(#{version_requirement}) does not appear to be loaded."
    message << "\n"
    message << "We did find (#{registered_entities.size}) registered entities in that package:\n\n"
    registered_entities.each do |other|
      message << ">> name: (#{other.name}) pkg_name: (#{other.pkg_name}) pkg_version: (#{other.pkg_version})\n"
    end
    message << "\n\nYou may need to update your Gemfile and run 'bundle install' "
    message << "to update your local gems.\n\n"
    raise Sprout::Errors::LoadError.new message
  end

  resolve_file_target entity
  entity
end

- (Object) register(entity)

Register a new feature for future lookups



48
49
50
51
52
# File 'lib/sprout/ruby_feature.rb', line 48

def register entity
  validate_registration entity
  registered_entities.unshift entity
  entity
end

- (Object) registered_entities (protected)

An entity has the following parameters: name pkg_name pkg_version platform



177
178
179
# File 'lib/sprout/ruby_feature.rb', line 177

def registered_entities
  @registered_entities ||= []
end

- (Object) require_ruby_package(name) (protected)



162
163
164
165
166
167
168
# File 'lib/sprout/ruby_feature.rb', line 162

def require_ruby_package name
  begin
    require name.to_s
  rescue LoadError => e
    raise Sprout::Errors::LoadError.new "Could not load the required file (#{name}) - Maybe you need to run 'gem install #{name}' or maybe 'bundle install'?"
  end
end

- (Object) resolve_file_target(entity) (protected)



90
91
92
93
94
95
96
97
98
99
# File 'lib/sprout/ruby_feature.rb', line 90

def resolve_file_target entity
  if(entity.respond_to? :resolve)
    entity.resolve
  elsif(entity.respond_to? :file_target)
    file_target = entity.file_target
    if(file_target.respond_to? :resolve)
      file_target.resolve
    end
  end
end

- (Boolean) satisfies_environment?(entity, environment) (protected)

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/sprout/ruby_feature.rb', line 129

def satisfies_environment? entity, environment
  #puts ">> env: #{entity.environment} vs. #{environment}"
  environment.nil? || !entity.respond_to?(:environment) || entity.environment.to_s == environment.to_s
end

- (Boolean) satisfies_name?(entity, expected) (protected)

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
# File 'lib/sprout/ruby_feature.rb', line 139

def satisfies_name? entity, expected
  #puts ">> name: #{entity.name} vs. #{expected}"
  return true if expected.nil? || !entity.respond_to?(:name)
  if expected.is_a?(Array)
    return expected.include? entity.name
  end
  return expected.to_s == entity.name.to_s
end

- (Boolean) satisfies_pkg_name?(entity, expected) (protected)

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/sprout/ruby_feature.rb', line 134

def satisfies_pkg_name? entity, expected
  #puts ">> pkg_name: #{entity.pkg_name} vs. #{expected}"
  expected.nil? || !entity.respond_to?(:pkg_name) || entity.pkg_name.to_s == expected.to_s
end

- (Boolean) satisfies_platform?(entity) (protected)

Returns:

  • (Boolean)


148
149
150
151
152
153
154
# File 'lib/sprout/ruby_feature.rb', line 148

def satisfies_platform? entity
  #puts">> satisfies platform?"
  # If not platform is defined, it's assumed to work on all platforms:
  return true if !entity.respond_to?(:platform) || entity.platform.nil?
  #puts ">> platform: #{entity.platform}"
  Sprout.current_system.can_execute?(entity.platform)
end

- (Boolean) satisfies_version?(entity, version_requirement = nil) (protected)

Returns:

  • (Boolean)


156
157
158
159
160
# File 'lib/sprout/ruby_feature.rb', line 156

def satisfies_version? entity, version_requirement=nil
  return true if version_requirement.nil?
  req_version = Gem::Requirement.create version_requirement
  req_version.satisfied_by?(Gem::Version.create(entity.pkg_version))
end

- (Object) update_registered_entities (protected)

Used by the Generator::Base to update inputs from empty class definitions to instances..



110
111
# File 'lib/sprout/ruby_feature.rb', line 110

def update_registered_entities
end

- (Object) validate_registration(entity) (protected)



101
102
103
104
105
# File 'lib/sprout/ruby_feature.rb', line 101

def validate_registration entity
  if(!entity.respond_to?(:name) || entity.name.nil?)
    raise Sprout::Errors::UsageError.new "Cannot register a RubyFeature without a 'name' getter"
  end
end