Class: Indexer::Resource

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

Overview

A resource encapsulates information about a URI related to a project.

TODO: This model is a little tricky b/c it's difficult to settle on a standard set of types or labels. It is also difficult to determine if label's and types should in fact be infered or just left empty if not provided. Any insight welcome!

Constant Summary collapse

TYPE_LABELS =

Recognized types and the default labels that do with them.

{
  'work'     => "Development",
  'dev'      => "Development",
  'code'     => "Source Code",
  'source'   => "Source Code",
  'bugs'     => "Issue Tracker",
  'issues'   => "Issue Tracker",
  'mail'     => "Mailing List",
  'list'     => "Mailing List",
  'forum'    => "Support Forum",
  'support'  => "Support Forum",
  'faq'      => "Fact Sheet",
  'home'     => "Homepage",
  'homepage' => "Homepage",
  'web'      => "Website",
  'website'  => "Website",
  'blog'     => "Blog",
  'pkg'      => "Download",
  'dist'     => "Download",
  'api'      => "API Guide",
  'irc'      => "IRC Channel",
  'chat'     => "IRC Channel",
  'doc'      => "Documentation",
  'docs'     => "Documentation",
  'wiki'     => "User Guide"
}
KNOWN_TYPES =
TYPE_LABELS.keys

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

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

Constructor Details

#initialize(data = {}) ⇒ Resource

Initialize new Resource instance.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/indexer/components/resource.rb', line 43

def initialize(data={})
  @data = {}

  data = data.rekey(&:to_sym)

  ## best to assign in this order
  self.uri   = data.delete(:uri)
  self.type  = data.delete(:type) if data.key?(:type)

  merge!(data)
end

Dynamic Method Handling

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

Instance Attribute Details

#labelObject Also known as: name

A label that can be used to identify the purpose of a particular repository.



59
60
61
# File 'lib/indexer/components/resource.rb', line 59

def label
  @label
end

#typeObject

Returns the value of attribute type.



97
98
99
# File 'lib/indexer/components/resource.rb', line 97

def type
  @type
end

#uriObject

The repository's URI.



83
84
85
# File 'lib/indexer/components/resource.rb', line 83

def uri
  @uri
end

Class Method Details

.parse(data) ⇒ Resource

Parse data returning a new Resource instance.

Parameters:

  • data (String, Array<String>, Array<Hash>, Hash)

    Resource information.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/indexer/components/resource.rb', line 17

def self.parse(data)
  case data
  when String
    new(:uri=>data)
  when Array
    h, d = {}, data.dup  # TODO: data.rekey(&:to_s)
    h.update(d.pop) while Hash === d.last
    if x = d.find{ |e| Valid.uri?(e) }
      h[:uri] = d.delete(x)
    end
    if x = d.find{ |e| Valid.word?(e) && e.downcase == e }
      h[:type] = d.delete(x)
    end
    h[:label] = d.shift unless d.empty?
    raise ValidationError, "malformed resource -- #{data.inspect}" unless d.empty?
    new(h)
  when Hash
    new(data)
  else
    raise(ValidationError, "not a valid resource")
  end
end

Instance Method Details

#infer_label(type) ⇒ Object (private)



168
169
170
# File 'lib/indexer/components/resource.rb', line 168

def infer_label(type)
  TYPE_LABELS[type.to_s.downcase] 
end

#infer_type(string) ⇒ Object (private)

TODO:

Deciding on a set of recognized types and inferences for them is rather tricky. Some sort of guiding design principle would be helpful.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/indexer/components/resource.rb', line 176

def infer_type(string)
  case string.to_s.downcase
  when /work/i, /dev/i
    'dev'  #'development'
  when /code/i, /source/i
    'code' #'source-code'
  when /bugs/i, /issue/i, /tracker/i
    'bugs' #'issue-tracker'
  when /mail/i, /list/i
    'mail' #'mailing-list'
  when /chat/i, /irc/i
    'chat' #'online-chat'
  when /forum/i, /support/i, /q\&a/i
    'help' #'support-forum'
  when /wiki/i, /user/i
    'wiki' #'wiki-wiki'
  when /blog/i, /weblog/i
    'blog' #'weblog'
  when /home/i, /website/i
    'home' #'homepage'
  when /gem/i, /packge/i, /dist/i, /download/i
    'dist' #'package' or 'distribution' ?
  when /api/i, /reference/i, /guide/i
    'api'  #'reference' ???
  when /doc/i
    'doc' #'documentation'
  else
    nil
  end
end