Class: Baltix::Spec::Rpm::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/baltix/spec/rpm/name.rb

Defined Under Namespace

Classes: InvalidAdoptedNameError, UnsupportedMatchError

Constant Summary collapse

PREFICES =
%w(gem ruby rubygem)
RULE =
/^(?<full_name>(?:(?<prefix>#{PREFICES.join('|')})-)?(?<name>.*?))(?:-(?<suffix>doc|devel))?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/baltix/spec/rpm/name.rb', line 8

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/baltix/spec/rpm/name.rb', line 8

def name
  @name
end

#suffixObject (readonly)

Returns the value of attribute suffix.



8
9
10
# File 'lib/baltix/spec/rpm/name.rb', line 8

def suffix
  @suffix
end

#support_nameObject

Returns the value of attribute support_name.



9
10
11
# File 'lib/baltix/spec/rpm/name.rb', line 9

def support_name
  @support_name
end

Class Method Details

.default_prefixObject



19
20
21
# File 'lib/baltix/spec/rpm/name.rb', line 19

def self.default_prefix
   "gem"
end

.parse(name_in, options_in = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/baltix/spec/rpm/name.rb', line 121

def parse name_in, options_in = {}
   m, kind =
      if name_in.is_a?(self)
         [name_in.original_fullname.match(RULE), name_in.kind]
      else
         [name_in.match(RULE)]
      end

   aliases_in = (options_in[:aliases] || []).flatten.uniq
   subaliases = aliases_in - [ m["full_name"] ]
   #aliases = subaliases | [ m["full_name"] ]

   raise(InvalidAdoptedNameError) if !m

   prefixed = subaliases.size >= aliases_in.size
   options = {
      prefix: prefixed && m["prefix"] || nil,
      #prefix: subaliases.blank? && m["prefix"] || nil,
      #prefix: m["prefix"],
      suffix: m["suffix"],
      kind: kind,
      #name: m["name"],
      name: prefixed && m["name"] || m["full_name"],
      #name: subaliases.blank? && m["name"] || m["full_name"],
   }.merge(options_in).merge({
      aliases: subaliases | [ m["full_name"] ]
   })

   options[:name] = options[:name].blank? && options[:aliases].first || options[:name]
   #binding.pry if name_in =~ /ruby/

   new(options)
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
# File 'lib/baltix/spec/rpm/name.rb', line 43

def == other
   eql?(other)
end

#===(other) ⇒ Object



47
48
49
# File 'lib/baltix/spec/rpm/name.rb', line 47

def === other
   eql?(other)
end

#=~(re) ⇒ Object



51
52
53
# File 'lib/baltix/spec/rpm/name.rb', line 51

def =~ re
   to_s =~ re
end

#aliasesObject



11
12
13
# File 'lib/baltix/spec/rpm/name.rb', line 11

def aliases
   @aliases ||= []
end

#autonameObject



98
99
100
# File 'lib/baltix/spec/rpm/name.rb', line 98

def autoname
   name&.downcase&.gsub(/[\._]/, "-")
end

#autoprefixObject



94
95
96
# File 'lib/baltix/spec/rpm/name.rb', line 94

def autoprefix
   %w(exec app).include?(kind) || %w(app).include?(support_name&.kind) ? nil : self.class.default_prefix
end

#autosuffixObject



102
103
104
# File 'lib/baltix/spec/rpm/name.rb', line 102

def autosuffix
   %w(doc devel).include?(kind) && kind || nil
end

#eql?(other, deep = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
# File 'lib/baltix/spec/rpm/name.rb', line 32

def eql? other, deep = false
   case other
   when self.class
      self.eql_by?(:kind, other) && self.eql_by?(:name, other)
   when String, Symbol
      (([ autoname, fullname ] | [ aliases ].flatten) & self.class.parse(other).aliases).any?
   else
      other.to_s == self.fullname
   end || deep && self.eql_by?(:support_name, other)
end

#eql_by?(value, other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baltix/spec/rpm/name.rb', line 68

def eql_by? value, other
   case value
   when :name
      ([ self.name, self.aliases ].flatten & [ other.name, other.aliases ].flatten).any?
   when :kind
      self.kind == other.kind
   when :support_name
      self.support_name === (other.is_a?(self.class) && other.support_name || other)
   else
      raise(UnsupportedMatchError.new)
   end
end

#fullnameObject

fullname returns newly reconstructed adopted full name based on the storen data. All the “.” and “_” is replaced with “-”, and “ruby” prefix with “gem”.

name.fullname #=> “gem-foo-bar-baz-doc”



86
87
88
# File 'lib/baltix/spec/rpm/name.rb', line 86

def fullname
   [ autoprefix, autoname, autosuffix ].compact.join("-")
end

#merge(other) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/baltix/spec/rpm/name.rb', line 59

def merge other
   options =
      %w(prefix suffix name support_name kind).map do |prop|
         [ prop.to_sym, other.send(prop) || self.send(prop) ]
      end.to_h

   self.class.new(options.merge(aliases: self.aliases | other.aliases))
end

#original_fullnameObject



90
91
92
# File 'lib/baltix/spec/rpm/name.rb', line 90

def original_fullname
   [ prefix, name, suffix ].compact.join("-")
end

#prefixObject



15
16
17
# File 'lib/baltix/spec/rpm/name.rb', line 15

def prefix
   @prefix ||= kind == "lib" && self.class.default_prefix || nil
end

#to_sObject



55
56
57
# File 'lib/baltix/spec/rpm/name.rb', line 55

def to_s
   fullname
end