Class: RBlade::AttributesManager

Inherits:
HtmlString show all
Defined in:
lib/rblade/helpers/attributes_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ AttributesManager

Returns a new instance of AttributesManager.



6
7
8
# File 'lib/rblade/helpers/attributes_manager.rb', line 6

def initialize attributes
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rblade/helpers/attributes_manager.rb', line 24

def method_missing(method, *, &)
  if [:select, :filter, :slice].include? method
    AttributesManager.new @attributes.send(method, *, &)
  else
    @attributes.send(method, *, &)
  end
end

Instance Method Details

#class(new_classes) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rblade/helpers/attributes_manager.rb', line 68

def class(new_classes)
  new_classes = ClassManager.new(new_classes).to_s
  attributes = @attributes.dup
  attributes[:class] = mergeClasses attributes[:class], new_classes

  AttributesManager.new attributes
end

#default(key, default = nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rblade/helpers/attributes_manager.rb', line 10

def default(key, default = nil)
  if @attributes[key].nil? && !default.nil?
    @attributes[key] = default
  end

  @attributes[key]
end

#except(keys) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/rblade/helpers/attributes_manager.rb', line 58

def except(keys)
  keys = if keys.is_a? Array
    keys.map(&:to_sym)
  else
    [keys.to_sym]
  end

  AttributesManager.new @attributes.except(*keys)
end

#has?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/rblade/helpers/attributes_manager.rb', line 18

def has?(*keys)
  keys.map!(&:to_sym)

  keys.all? { |key| @attributes.has_key? key }
end

#has_any?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/rblade/helpers/attributes_manager.rb', line 96

def has_any?(*keys)
  keys.map!(&:to_sym)

  keys.any? { |key| @attributes.has_key? key }
end

#merge(default_attributes) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rblade/helpers/attributes_manager.rb', line 76

def merge(default_attributes)
  new_attributes = default_attributes

  @attributes.each do |key, value|
    if key == :class && !new_attributes[key].nil?
      new_attributes[key] = mergeClasses(new_attributes[key], value.to_s)
      next
    end

    if key == :style && !new_attributes[key].nil?
      new_attributes[key] = mergeStyles(new_attributes[key], value.to_s)
      next
    end

    new_attributes[key] = value
  end

  AttributesManager.new new_attributes
end

#only(keys) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/rblade/helpers/attributes_manager.rb', line 48

def only(keys)
  keys = if keys.is_a? Array
    keys.map(&:to_sym)
  else
    [keys.to_sym]
  end

  AttributesManager.new @attributes.slice(*keys)
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rblade/helpers/attributes_manager.rb', line 32

def respond_to_missing?(method_name, *args)
  @attributes.respond_to?(method_name)
end

#to_s(attributes = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rblade/helpers/attributes_manager.rb', line 36

def to_s attributes = nil
  attributes ||= @attributes

  attributes.map do |key, value|
    "#{key}=\"#{(value == true) ? key : h(value)}\""
  end.join " "
end

#to_strObject



44
45
46
# File 'lib/rblade/helpers/attributes_manager.rb', line 44

def to_str
  to_s
end