Class: TogoStanza::Stanza::Base

Inherits:
Object
  • Object
show all
Extended by:
ExpressionMap::Macro
Includes:
Grouping, Querying
Defined in:
lib/togostanza/stanza/base.rb

Constant Summary

Constants included from Querying

Querying::MAPPINGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExpressionMap::Macro

define_expression_map

Methods included from Grouping

_grouping, grouping

Methods included from Querying

#query

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



186
187
188
# File 'lib/togostanza/stanza/base.rb', line 186

def initialize(params = {})
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



190
191
192
# File 'lib/togostanza/stanza/base.rb', line 190

def params
  @params
end

Class Method Details

.idObject



180
181
182
# File 'lib/togostanza/stanza/base.rb', line 180

def self.id
  to_s.underscore.sub(/_stanza$/, '')
end

Instance Method Details

#contextObject



192
193
194
# File 'lib/togostanza/stanza/base.rb', line 192

def context
  Hashie::Mash.new(properties.resolve_all_in_parallel(self, params))
end

#metadata(server_url) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/togostanza/stanza/base.rb', line 206

def (server_url)
  path = File.join(root, 'metadata.json')

  if File.exist?(path)
    orig = JSON.load(open(path))
    stanza_uri = "#{server_url}/#{orig['id']}"

    usage_attrs = orig['parameter'].map {|hash|
      unless hash['key'].start_with?("data-stanza-") then
        hash['key'] = "data-stanza-" <<  hash['key']
      end
      "#{hash['key']}=\"#{hash['example']}\""
    }.push("data-stanza=\"#{stanza_uri}\"").join(' ')

    append_prefix_to_hash_keys(orig.merge(usage: "<div #{usage_attrs}></div>"), 'stanza').merge('@id' => stanza_uri)
  else
    nil
  end
end

#renderObject



200
201
202
203
204
# File 'lib/togostanza/stanza/base.rb', line 200

def render
  path = File.join(root, 'template.hbs')

  Tilt.new(path).render(context)
end

#resource(name) ⇒ Object



196
197
198
# File 'lib/togostanza/stanza/base.rb', line 196

def resource(name)
  resources.resolve(self, name, params)
end