Class: Indexer::Requirement

Inherits:
Model
  • Object
show all
Defined in:
lib/indexer/components/requirement.rb

Overview

Requirement class.

QUESTION: Does Requirement really need to handle multiple version constraints? Currently this only supports one version constraint.

Direct Known Subclasses

Dependency

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#[], #[]=, attr_reader, attr_writer, #key?, #merge!, #method_missing, #store, #to_yaml, #validate

Constructor Details

#initialize(name, specifics = {}) ⇒ Requirement (private)

Create new instance of Requirement.



102
103
104
105
# File 'lib/indexer/components/requirement.rb', line 102

def initialize(name, specifics={})
  specifics[:name] = name
  super(specifics)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Indexer::Model

Instance Attribute Details

#enginesArray Also known as: engine

Applies only for specified Ruby engines. Each entry can be the RUBY_ENGINE value and optionally a version constraint on RUBY_VERSION.

Returns:

  • (Array)

    name and version constraint



243
244
245
# File 'lib/indexer/components/requirement.rb', line 243

def engines
  @engines
end

#groupsArray Also known as: group

The groups to which the requirement belongs.

Returns:

  • (Array)

    list of groups



179
180
181
# File 'lib/indexer/components/requirement.rb', line 179

def groups
  @groups
end

#nameObject

Returns the value of attribute name.



121
122
123
# File 'lib/indexer/components/requirement.rb', line 121

def name
  @name
end

#platformsObject Also known as: platform

Returns the value of attribute platforms.



279
280
281
# File 'lib/indexer/components/requirement.rb', line 279

def platforms
  @platforms
end

#repositoryObject Also known as: repo

The public repository resource in which the requirement source code can be found.



299
300
301
# File 'lib/indexer/components/requirement.rb', line 299

def repository
  @repository
end

#sourcesObject

Places from which the requirement can be obtained. Generally a source should be a URI, but there is no strict requirement. It can be as simple as a name, e.g. rubygems or as specific as a URL to a downloadable .zip package.

Examples:

requirement.sources = ['http://rubygems.org']


321
322
323
# File 'lib/indexer/components/requirement.rb', line 321

def sources
  @sources
end

#versionVersion::Constraint

The requirement's version constraint.

Returns:



134
135
136
# File 'lib/indexer/components/requirement.rb', line 134

def version
  @version
end

Class Method Details

.parse(data) ⇒ Object

Parse data into a Requirement instance.

TODO: What about respond_to?(:to_str) for String, etc.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/indexer/components/requirement.rb', line 15

def self.parse(data)
  case data
  when String
    parse_string(data)
  when Array
    parse_array(data)
  when Hash
    parse_hash(data)
  else
    raise(ValidationError, "requirement")
  end
end

.parse_array(array) ⇒ Object (private)



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/indexer/components/requirement.rb', line 81

def self.parse_array(array)
  name, data = *array
  case data
  when Hash
    groups = []
    groups << (data.delete('groups') || data.delete(:groups))
    groups << (data.delete('group')  || data.delete(:group))
    groups = groups.flatten.compact.map{ |g| g.to_s }
    data[:groups] = groups

    new(name.to_s, data)
  when String
    parse_string(name + " " + data)
  else
    raise(ValidationError, "requirement")
  end
end

.parse_hash(data) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/indexer/components/requirement.rb', line 32

def self.parse_hash(data)
  name = (data.delete('name') || data.delete(:name)).to_s
  # make sure groups are strings
  groups = []
  groups << (data.delete('groups') || data.delete(:groups))
  groups << (data.delete('group')  || data.delete(:group))
  groups = groups.flatten.compact.map{ |g| g.to_s }
  data[:groups] = groups
  #
  new(name, data)
end

.parse_string(string) ⇒ Object (private)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/indexer/components/requirement.rb', line 46

def self.parse_string(string)
  case string.strip
  when /^(\w.*?)\s+(.*?)\s*\((.*?)\)$/
    name    = $1
    version = $2
    groups  = $3
  when /^(\w.*?)\s+(.*?)$/
    name    = $1
    version = $2
    groups  = nil
  when /^(\w.*?)$/
    name    = $1
    version = nil
    groups  = nil
  else
    raise(ValidationError, "requirement")
  end

  version = nil                          if version.to_s.strip.empty?
  groups = groups.split(/\s*[,;]?\s+/) if groups

  specifics = {}
  specifics['version']     = version  if version
  specifics['groups']      = groups   if groups

  if groups && !groups.empty? && !groups.include?('runtime')
    specifics['development'] = true     
  end

  new(name, specifics)
end

Instance Method Details

#development=(boolean) ⇒ Object

Set the requirement's development flag.

Parameters:

  • true/false (Boolean)

    development requirement



162
163
164
# File 'lib/indexer/components/requirement.rb', line 162

def development=(boolean)
  @data[:development] = !!boolean
end

#development?Boolean

Returns true if the requirement is a development requirement.

Returns:

  • (Boolean)

    development requirement?



153
154
155
# File 'lib/indexer/components/requirement.rb', line 153

def development?
  @data[:development]
end

#external=(boolean) ⇒ Object

Set external.

Parameters:

  • external (Boolean)

    requirement?



232
233
234
# File 'lib/indexer/components/requirement.rb', line 232

def external=(boolean)
  @data[:external] = !!boolean
end

#external?Boolean

Is the requirment external? An external requirement is one that is only available outside the expected packaging system. For a Ruby application, for example, this would be library not available via rubygems.org, such as a C library that is only avaialble via an operating system's package manager or via a direct download using the "make; make install" compile and installation procedure.

Returns:

  • (Boolean)

    external requirement?



223
224
225
# File 'lib/indexer/components/requirement.rb', line 223

def external?
  @data[:external]
end

#initialize_attributesObject (private)



108
109
110
111
112
113
114
115
# File 'lib/indexer/components/requirement.rb', line 108

def initialize_attributes
  @data = {
    :groups    => [],
    :engines   => [],
    :platforms => [],
    :sources   => []
  }
end

#optional=(boolean) ⇒ Object

Set optional.

Parameters:

  • optional (Boolean)

    requirement?



210
211
212
# File 'lib/indexer/components/requirement.rb', line 210

def optional=(boolean)
  @data[:optional] = !!boolean
end

#optional?Boolean

Is the requirment optional? An optional requirement is recommended but not strictly necessary.

Returns:

  • (Boolean)

    optional requirement?



201
202
203
# File 'lib/indexer/components/requirement.rb', line 201

def optional?
  @data[:optional]
end

#runtime?Boolean

Return true if requirement is a runtime requirement.

Returns:

  • (Boolean)

    runtime requirement?



170
171
172
# File 'lib/indexer/components/requirement.rb', line 170

def runtime?
 ! @data[:development]
end

#to_hObject

Convert to canonical hash.



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/indexer/components/requirement.rb', line 333

def to_h
  h = super

  h['version']     = version.to_s if version
  h['repository']  = repository.to_h if repository
  h['engines']     = engines.map{ |e| e.to_h }

  h.delete('groups')    if h['groups']    && h['groups'].empty?
  h.delete('engines')   if h['engines']   && h['engines'].empty?
  h.delete('platforms') if h['platforms'] && h['platforms'].empty?
  h.delete('sources')   if h['sources']   && h['sources'].empty?

  h
end