Class: Hud::Display::Component

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



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

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

Instance Method Details

#development?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/hud.rb', line 113

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

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



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

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 Oga.parse_html(result).css(css) if css
  result
end

#foldersObject



109
110
111
# File 'lib/hud.rb', line 109

def folders
  Hud.configuration.components_dirs
end

#production?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/hud.rb', line 129

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

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/hud.rb', line 141

def render_template(name: nil, from: nil, locals: {},css: nil)
  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 Oga.parse_html(result).css(css) if css
        return result
      else
        from_path = base_path.join(from, "components")
        result = template.render(self, locals)
        return Oga.parse_html(result).css(css) if css
        return result if path.to_path.start_with? from_path.to_s
      end

    end
  end

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

#staging?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/hud.rb', line 133

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