Class: Reality::Git::AttributeRule

Inherits:
Object
  • Object
show all
Defined in:
lib/reality/git/attribute_rule.rb

Overview

Represents a rule within the attributes file

Constant Summary collapse

ATTR_ORDER =
%w(text binary eol encoding eofnl)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, attributes) ⇒ AttributeRule

Returns a new instance of AttributeRule.


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/reality/git/attribute_rule.rb', line 21

def initialize(pattern, attributes)
  @pattern = pattern.gsub('[[:space:]]', ' ')
  @attributes = {}
  @priority = 1
  attributes.each do |k, v|
    if k.to_s == 'priority'
      @priority = v
    else
      @attributes[k.to_s] = v
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.


35
36
37
# File 'lib/reality/git/attribute_rule.rb', line 35

def attributes
  @attributes
end

#patternObject (readonly)

Returns the value of attribute pattern.


34
35
36
# File 'lib/reality/git/attribute_rule.rb', line 34

def pattern
  @pattern
end

#priorityObject (readonly)

Returns the value of attribute priority.


36
37
38
# File 'lib/reality/git/attribute_rule.rb', line 36

def priority
  @priority
end

Class Method Details

.parse_line(line) ⇒ Object


85
86
87
88
89
90
# File 'lib/reality/git/attribute_rule.rb', line 85

def parse_line(line)
  line = line.strip
  return nil if line.start_with?('#') || line.empty?
  pattern, attrs = line.strip.split(/\s+/, 2)
  AttributeRule.new(pattern, attrs ? parse_attributes(attrs) : {})
end

Instance Method Details

#<=>(other) ⇒ Object


75
76
77
78
79
80
81
82
# File 'lib/reality/git/attribute_rule.rb', line 75

def <=>(other)
  order = self.priority <=> other.priority
  if 0 != order
    order
  else
    to_s <=> other.to_s
  end
end

#attr_value(key, value) ⇒ Object

noinspection RubySimplifyBooleanInspection


57
58
59
60
61
62
63
64
65
# File 'lib/reality/git/attribute_rule.rb', line 57

def attr_value(key, value)
  if true == value
    " #{key}"
  elsif false == value
    " -#{key}"
  else
    " #{key}=#{value}"
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/reality/git/attribute_rule.rb', line 67

def eql?(other)
  self.pattern == other.pattern && self.attributes == other.attributes && self.priority == other.priority
end

#hashObject


71
72
73
# File 'lib/reality/git/attribute_rule.rb', line 71

def hash
  self.pattern.hash + self.attributes.hash + self.priority
end

#to_sObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reality/git/attribute_rule.rb', line 38

def to_s
  rule = self.pattern.gsub(' ', '[[:space:]]')

  attributes = self.attributes.dup
  ATTR_ORDER.each do |key|
    unless attributes[key].nil?
      rule = "#{rule}#{attr_value(key, attributes[key])}"
    end
  end
  attributes.keys.sort.each do |key|
    unless ATTR_ORDER.include?(key)
      rule = "#{rule}#{attr_value(key, attributes[key])}"
    end
  end

  rule
end