Class: Orange::Resource

Inherits:
Object show all
Extended by:
ClassInheritableAttributes
Defined in:
lib/orange-core/resource.rb

Overview

Orange Resource for being subclassed

Direct Known Subclasses

Mapper, NotFound, PageParts, Parser, RoutableResource, Scaffold

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassInheritableAttributes

cattr_accessor, cattr_reader, cattr_writer, eval_in_accessor_module, fetch_value, store_value

Constructor Details

#initialize(*args, &block) ⇒ Resource

Returns a new instance of Resource.



21
22
23
24
# File 'lib/orange-core/resource.rb', line 21

def initialize(*args, &block)
  @options = DefaultHash.new.merge!(Options.new(*args, &block).hash)
  self.class.viewable_actions ||= []
end

Class Method Details

.call_me(name) ⇒ Object



45
46
47
# File 'lib/orange-core/resource.rb', line 45

def self.call_me(name)
  self.called = name
end

.set_orange(*args) ⇒ Object



34
35
36
# File 'lib/orange-core/resource.rb', line 34

def self.set_orange(*args)
  raise 'instantiate the resource before calling set orange'
end

.viewable(*args) ⇒ Object



16
17
18
19
# File 'lib/orange-core/resource.rb', line 16

def self.viewable(*args)
  self.viewable_actions ||= []
  args.each{|arg| self.viewable_actions << arg}
end

Instance Method Details

#afterLoadObject



42
43
# File 'lib/orange-core/resource.rb', line 42

def afterLoad
end

#do_view(packet, mode, *args) ⇒ String

Renders a view, with all options set for haml to access. Calls #view_opts to generate the haml options.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • args (optional, Array)

    the args array

Returns:

  • (String)

    haml parsed string to be placed in packet by #route



89
90
91
92
# File 'lib/orange-core/resource.rb', line 89

def do_view(packet, mode, *args)
  haml_opts = view_opts(packet, mode, *args)
  orange[:parser].haml("#{mode.to_s}.haml", packet, haml_opts)
end

#find_extras(packet, mode) ⇒ Hash

Returns a hash of extra options to be set and made available by the haml parser. Overriding this method is useful for passing extra bits of info to rendering for certain view modes without rewriting all of the other scaffolding

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

Returns:

  • (Hash)

    a hash of extras to be included in the rendering



117
118
119
# File 'lib/orange-core/resource.rb', line 117

def find_extras(packet, mode)
  {}
end

#initObject



38
39
40
# File 'lib/orange-core/resource.rb', line 38

def init
  afterLoad
end

#nestsObject



12
13
14
# File 'lib/orange-core/resource.rb', line 12

def nests
  {}
end

#optionsObject



79
80
81
# File 'lib/orange-core/resource.rb', line 79

def options
  @options 
end

#orangeObject



49
50
51
# File 'lib/orange-core/resource.rb', line 49

def orange
  @orange
end

#orange_nameObject



75
76
77
# File 'lib/orange-core/resource.rb', line 75

def orange_name
  @my_orange_name || self.class.called || false
end

#routableObject



53
54
55
# File 'lib/orange-core/resource.rb', line 53

def routable
  false
end

#set_orange(orange, name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/orange-core/resource.rb', line 26

def set_orange(orange, name)
  @orange = orange
  @my_orange_name = name
  init
  orange.register(:stack_loaded) { |s| stack_init } if self.respond_to? :stack_init
  self
end

#view(packet = false, *args) ⇒ Object



57
58
59
60
61
62
# File 'lib/orange-core/resource.rb', line 57

def view(packet = false, *args)
  opts = args.extract_options!
  my_action = packet['route.resource_action'] if packet
  action = opts[:mode] || opts[:resource_action] || my_action || :index
  viewable(packet, action, opts)
end

#view_opts(packet, mode, *args) ⇒ Hash

Returns the options for including in template rendering. All keys passed in the args array will automatically be local variables in the haml template. In addition, the props, resource, and model_name variables will be available.

Parameters:

  • packet (Orange::Packet)

    the packet we are returning a view for

  • mode (Symbol)

    the mode we are trying to view (used to find template name)

  • is_list (boolean)

    whether we want a list or not (view_opts will automatically look up a single object or a list of objects, so we need to know which)

  • args (optional, Array)

    the args array

Returns:

  • (Hash)

    hash of options to be used



104
105
106
107
108
109
# File 'lib/orange-core/resource.rb', line 104

def view_opts(packet, mode, *args)
  opts = args.extract_options!.with_defaults({:path => ''})
  all_opts = {:resource => self, :model_name => @my_orange_name}.merge!(opts)
  all_opts.with_defaults! find_extras(packet, mode)
  all_opts
end

#viewable(packet, mode, opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/orange-core/resource.rb', line 64

def viewable(packet, mode, opts={})
  self.class.viewable_actions ||= []
  if(self.respond_to?(mode))
    self.__send__(mode, packet, opts)
  elsif(self.class.viewable_actions.include?(mode))
    do_view(packet, mode, opts)
  else
    ''
  end
end