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.



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

def initialize(attributes)
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



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

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



73
74
75
76
77
78
79
# File 'lib/rblade/helpers/attributes_manager.rb', line 73

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

  AttributesManager.new attributes
end

#default(key, default = nil) ⇒ Object



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

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

  @attributes[key]
end

#except(keys) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rblade/helpers/attributes_manager.rb', line 63

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)


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

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

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

#has_any?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/rblade/helpers/attributes_manager.rb', line 101

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

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

#html_safe?Boolean

Returns:

  • (Boolean)


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

def html_safe?
  true
end

#merge(default_attributes) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rblade/helpers/attributes_manager.rb', line 81

def merge(default_attributes)
  new_attributes = default_attributes

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

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

    new_attributes[key] = value
  end

  AttributesManager.new new_attributes
end

#only(keys) ⇒ Object



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

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)


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

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

#to_sObject



49
50
51
# File 'lib/rblade/helpers/attributes_manager.rb', line 49

def to_s
  self
end

#to_str(attributes = nil) ⇒ Object



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

def to_str(attributes = nil)
  attributes ||= @attributes

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