Class: CRUDtree::Node
- Inherits:
-
Object
- Object
- CRUDtree::Node
- Defined in:
- lib/crudtree/tree/node.rb
Instance Attribute Summary collapse
-
#default_collection ⇒ Object
readonly
Returns the value of attribute default_collection.
-
#default_member ⇒ Object
readonly
Returns the value of attribute default_member.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_call ⇒ Object
readonly
Returns the value of attribute parent_call.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#subnodes ⇒ Object
readonly
Returns the value of attribute subnodes.
Instance Method Summary collapse
-
#collection(params) ⇒ Object
Creates a new endnode with type collection.
- #collection? ⇒ Boolean
-
#endnode(params) ⇒ Object
Creates a new End and attaches it to this Node.
- #endnodes ⇒ Object
-
#initialize(parent, params, &block) ⇒ Node
constructor
The params Hash takes the following keys:.
-
#member(params) ⇒ Object
Creates a new endnode with type member.
- #node(params, &block) ⇒ Object
- #nodes ⇒ Object
- #parent_is_master? ⇒ Boolean
- #parents ⇒ Object
-
#path ⇒ Object
Duck typing used for generation.
- #subs ⇒ Object
Constructor Details
#initialize(parent, params, &block) ⇒ Node
The params Hash takes the following keys:
:klass The object where to send the method that is returned by the router. Mostly a class, therefore it’s called ‘class_name’. Defaults to nil, but the interface may complain. You have been warned ;). May be used by the interface as needed (Rango wants Class.send :dispatcher, :send_method)
:identifier The identifier used to identify a resource, aka /user/:name instead of /user/:id (default).
:default_collection The collection that is called when nothing is given. Defaults to :index.
:default_member The member which is chosen when no method is given. Defaults to :show.
:paths Specify the path(s) you want to call this resource with. Defaults to klass.to_s.downcase The first one is used for generation.
Options used for generating
:model The name of the model. Needed. We don’t do magic here.
:parent_call The method to call on the model object to get its parent (for nested resources). Defaults to :model.downcase of the parent.
:name Symbol used to identify the node when generating a collection route. Defaults to Klass.to_s.downcase.to_sym
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/crudtree/tree/node.rb', line 40 def initialize(parent, params, &block) @klass = params[:klass] @identifier = params[:identifier] || :id @default_collection = params[:default_collection] || :index @default_member = params[:default_member] || :show @paths = if params[:paths] [params[:paths]].flatten elsif params[:klass] [params[:klass].to_s.downcase.split("::").last] else raise ArgumentError, "No paths given" end @subnodes = [] @parent = parent # default routes @subnodes.unshift(EndNode.new(self, type: :member, call: :show, path: "", rest: :get)) @subnodes.unshift(EndNode.new(self, type: :collection, call: :index, path: "", rest: :get)) # generating unless @model = params[:model] raise(ArgumentError, "No model given.") end @parent_call = if params[:parent_call] params[:parent_call] elsif ! parent_is_master? parent.model.to_s.split('::').last.downcase else nil end @name = params[:name] || klass.to_s.downcase.to_sym block ? instance_eval(&block) : raise(ArgumentError, "No block given.") end |
Instance Attribute Details
#default_collection ⇒ Object (readonly)
Returns the value of attribute default_collection.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def default_collection @default_collection end |
#default_member ⇒ Object (readonly)
Returns the value of attribute default_member.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def default_member @default_member end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def identifier @identifier end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def klass @klass end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def parent @parent end |
#parent_call ⇒ Object (readonly)
Returns the value of attribute parent_call.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def parent_call @parent_call end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def paths @paths end |
#subnodes ⇒ Object (readonly)
Returns the value of attribute subnodes.
72 73 74 |
# File 'lib/crudtree/tree/node.rb', line 72 def subnodes @subnodes end |
Instance Method Details
#collection(params) ⇒ Object
Creates a new endnode with type collection. See EndNode.
85 86 87 |
# File 'lib/crudtree/tree/node.rb', line 85 def collection(params) endnode(params.merge({type: :collection})) end |
#collection? ⇒ Boolean
118 119 120 |
# File 'lib/crudtree/tree/node.rb', line 118 def collection? false end |
#endnode(params) ⇒ Object
Creates a new End and attaches it to this Node.
75 76 77 |
# File 'lib/crudtree/tree/node.rb', line 75 def endnode(params) @subnodes << EndNode.new(self, params) end |
#endnodes ⇒ Object
101 102 103 |
# File 'lib/crudtree/tree/node.rb', line 101 def endnodes subnodes.select{|subnode| subnode.is_a? EndNode} end |
#member(params) ⇒ Object
Creates a new endnode with type member. See Endnode.
80 81 82 |
# File 'lib/crudtree/tree/node.rb', line 80 def member(params) endnode(params.merge({type: :member})) end |
#node(params, &block) ⇒ Object
89 90 91 |
# File 'lib/crudtree/tree/node.rb', line 89 def node(params, &block) @subnodes << Node.new(self, params, &block) end |
#nodes ⇒ Object
97 98 99 |
# File 'lib/crudtree/tree/node.rb', line 97 def nodes subnodes.select{|subnode| subnode.is_a? Node} end |
#parent_is_master? ⇒ Boolean
105 106 107 |
# File 'lib/crudtree/tree/node.rb', line 105 def parent_is_master? ! parent.respond_to? :parent end |
#parents ⇒ Object
93 94 95 |
# File 'lib/crudtree/tree/node.rb', line 93 def parents [find_parent(self)].flatten[0..-2] end |
#path ⇒ Object
Duck typing used for generation
110 111 112 |
# File 'lib/crudtree/tree/node.rb', line 110 def path @paths.first end |
#subs ⇒ Object
114 115 116 |
# File 'lib/crudtree/tree/node.rb', line 114 def subs endnodes end |