Class: Orange::Resource

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

Overview

Orange Resource for being subclassed

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.



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

def initialize(*args, &block)
  @options = DefaultHash.new.merge!(Options.new(*args, &block).hash)
end

Class Method Details

.call_me(name) ⇒ Object



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

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

.set_orange(*args) ⇒ Object



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

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

Instance Method Details

#afterLoadObject



31
32
# File 'lib/orange-core/resource.rb', line 31

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



64
65
66
67
# File 'lib/orange-core/resource.rb', line 64

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



92
93
94
# File 'lib/orange-core/resource.rb', line 92

def find_extras(packet, mode)
  {}
end

#initObject



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

def init
  afterLoad
end

#optionsObject



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

def options
  @options 
end

#orangeObject



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

def orange
  @orange
end

#orange_nameObject



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

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

#routableObject



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

def routable
  false
end

#set_orange(orange, name) ⇒ Object



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

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



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

def view(packet = false, *args)
  ''
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



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

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