Class: Hud::Display::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/hud.rb

Defined Under Namespace

Classes: HtmlContent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/hud.rb', line 128

def method_missing(method_name, *args, &block)
  if @locals.respond_to?(method_name)
    @locals.send(method_name, *args, &block)
  else
    super
  end
  
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



105
106
107
# File 'lib/hud.rb', line 105

def content
  @content
end

#localsObject (readonly) Also known as: args

Returns the value of attribute locals.



104
105
106
# File 'lib/hud.rb', line 104

def locals
  @locals
end

Class Method Details

.call(locals: {}) ⇒ Object



159
160
161
# File 'lib/hud.rb', line 159

def self.call(locals: {})
  new(locals: locals)
end

Instance Method Details

#development?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/hud.rb', line 124

def development?
  ENV["RACK_ENV"] == "development"
end

#display(name, locals: {}, css: nil) ⇒ Object Also known as: render, d, r



141
142
143
144
145
# File 'lib/hud.rb', line 141

def display(name, locals: {},css:nil)
  template = Tilt::ERBTemplate.new("#{Hud.configuration.base_path}/components/#{name.to_s}.html.erb")
  result = template.render(self, locals)
  return HtmlContent.new(result)
end

#foldersObject



120
121
122
# File 'lib/hud.rb', line 120

def folders
  Hud.configuration.components_dirs
end

#production?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/hud.rb', line 151

def production?
  ENV["RACK_ENV"] == "production"
end

#render_template(name: nil, from: nil, locals: {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/hud.rb', line 163

def render_template(name: nil, from: nil, locals: {})
  name ||= self.class.to_s.downcase.gsub("::", "_")

  base_path = Pathname.new(Rack::App::Utils.pwd)

  paths_to_check = []

  folders.each do |folder_name|
    paths_to_check << base_path.join(folder_name, "components", "#{name}.html.erb")
  end

  root_component_path = base_path.join("components", "#{name}.html.erb")
  paths_to_check << root_component_path

  paths_to_check.each do |path|
    if File.exist?(path)
      template = Tilt::ERBTemplate.new(path)

      puts path
      if from.nil?
        result = template.render(self, locals)
        return HtmlContent.new(result)
      else
        from_path = base_path.join(from, "components")
        result = template.render(self, locals)
        return HtmlContent.new(result) if path.to_path.start_with? from_path.to_s
      end

    end
  end

  raise "cant find #{name} in #{paths_to_check.join(",")}"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/hud.rb', line 137

def respond_to_missing?(method_name, include_private = false)
  @locals.respond_to?(method_name) || super
end

#staging?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/hud.rb', line 155

def staging?
  ENV["RACK_ENV"] == "staging"
end