Class: RBlade::AttributesManager

Inherits:
Object
  • Object
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.



4
5
6
# File 'lib/rblade/helpers/attributes_manager.rb', line 4

def initialize attributes
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



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

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

Instance Method Details

#default(key, default = nil) ⇒ Object



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

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

  @attributes[key]
end

#except(keys) ⇒ Object



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

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)


16
17
18
# File 'lib/rblade/helpers/attributes_manager.rb', line 16

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

#merge(default_attributes) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rblade/helpers/attributes_manager.rb', line 56

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



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

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)


24
25
26
# File 'lib/rblade/helpers/attributes_manager.rb', line 24

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

#to_s(attributes = nil) ⇒ Object



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

def to_s attributes = nil
  attributes ||= @attributes

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