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



22
23
24
# File 'lib/rblade/helpers/attributes_manager.rb', line 22

def method_missing(method, *)
  @attributes.send(method, *)
end

Instance Method Details

#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



52
53
54
55
56
57
58
59
60
# File 'lib/rblade/helpers/attributes_manager.rb', line 52

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

  self.class.new @attributes.except(*keys)
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def has?(key)
  !@attributes[key].nil?
end

#merge(default_attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rblade/helpers/attributes_manager.rb', line 62

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

  self.class.new new_attributes
end

#only(keys) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/rblade/helpers/attributes_manager.rb', line 42

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

  self.class.new @attributes.slice(*keys)
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rblade/helpers/attributes_manager.rb', line 26

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

#to_s(attributes = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rblade/helpers/attributes_manager.rb', line 30

def to_s attributes = nil
  attributes ||= @attributes

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

#to_strObject



38
39
40
# File 'lib/rblade/helpers/attributes_manager.rb', line 38

def to_str
  to_s
end