Skip to content

Commit

Permalink
Basic Introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
teliosdev committed Nov 15, 2014
1 parent 2b4d105 commit 88e135c
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lib/curly/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ def presenter_for_name(name)
end
end

def presenter_for?(name)
presenter_for_name(name)
true
rescue NameError
false
end

# Whether a component is available to templates rendered with the presenter.
#
# Templates have components which correspond with methods defined on
Expand All @@ -199,6 +206,61 @@ def available_components
methods.map(&:to_s)
end

# Gives a description of the presenter. Gives information about its
# components, and what parameters they are allowed.
#
# Returns a Hash.
def description
result = {}
components = result[:components] = []

available_components.each do |component|
data = { name: component, type: "", parameters: [] }

if component.end_with?("?")
data[:type] = "conditional"
elsif presenter_for?(component)
data[:type] = "context"
elsif presenter_for?(component.singularize)
data[:type] = "collection"
else
data[:type] = "value"
end

instance_method(component.intern).parameters.each do |param|
add = {}
add[:name] = param[1].to_s

case param[0]
when :keyreq
add[:type] = "keyword"
add[:required] = true
when :req
add[:type] = "normal"
add[:required] = true
when :key
add[:type] = "keyword"
add[:required] = false
when :opt
add[:type] = "normal"
add[:required] = false
when :block
add[:type] = "block"
add[:required] = false
else
add[:type] = "unknown"
add[:required] = false
end

data[:parameters] << add
end

components << data
end

result
end

# The set of view paths that the presenter depends on.
#
# Example
Expand Down
55 changes: 55 additions & 0 deletions spec/presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ def monkey
presents :elephant, default: "Dumbo"

attr_reader :midget, :clown, :elephant

def alpha(name, age = 12)
name
end

def beta(test:, this: "thing")
test + this
end

def charlie(&test)
end

def delta?
false
end

def cats
end

class CatPresenter < Curly::Presenter; end
end

class FrenchCircusPresenter < CircusPresenter
Expand Down Expand Up @@ -112,6 +132,41 @@ class CircusPresenter::MonkeyPresenter < Curly::Presenter
end
end

describe ".description" do
it "gives a hash" do
CircusPresenter.description.should be_a Hash
end

it "describes the components" do
description = CircusPresenter.description

description[:components].should have(9).items
description[:components].should == [
{ name: "midget", type: "value", parameters: [] },
{ name: "clown", type: "value", parameters: [] },
{ name: "elephant", type: "value", parameters: [] },

{ name: "alpha", type: "value", parameters: [
{ name: "name", type: "normal", required: true },
{ name: "age", type: "normal", required: false }
]},

{ name: "beta", type: "value", parameters: [
{ name: "test", type: "keyword", required: true },
{ name: "this", type: "keyword", required: false }
]},

{ name: "charlie", type: "value", parameters: [
{ name: "test", type: "block", required: false }
]},

{ name: "delta?", type: "conditional", parameters: [] },
{ name: "cats", type: "collection", parameters: [] },
{ name: "monkey", type: "context", parameters: [] }
]
end
end

describe ".version" do
it "sets the version of the presenter" do
presenter1 = Class.new(Curly::Presenter) do
Expand Down

0 comments on commit 88e135c

Please sign in to comment.