Class: Orange::Carton
- Extended by:
- ClassInheritableAttributes
- Includes:
- DataMapper::Types
- Defined in:
- lib/orange-core/carton.rb
Overview
Orange::Carton is the main model class for Orange. It’s based on Datamapper. In addition to handling the database interactions, the carton keeps track of declared properties, which is used to create scaffolds.
All subclasses should start by declaring the “id” attribute. All models are assumed to have an id attribute by most everything else, so it’s a good idea to have one. Also, we use this to tie in initialization before using the other dsl-ish methods (since these other methods are class level and would happen before initialize ever gets called)
Orange::Carton adds many shortcut methods for adding various datatypes to the model in a more declarative style (‘id` vs `property :id, Serial`)
For classes that don’t need anything but scaffolding, there’s the as_resource method, which automatically creates a scaffolding resource for the model.
A model that doesn’t need scaffolded at all could optionally forgo the carton class and just include DataMapper::Resource. All carton methods are to improve scaffolding capability.
Constant Summary collapse
- SCAFFOLD_OPTIONS =
[:display_name, :levels]
Class Method Summary collapse
- .add_scaffold(name, type, dm_type, opts) ⇒ Object
-
.admin(&block) ⇒ Object
Helper to wrap properties into admin level.
-
.admin_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does.
-
.as_resource ⇒ Object
Declares a ModelResource subclass that scaffolds this carton The Subclass will have the name of the carton followed by “_Resource”.
-
.boolean(name, opts = {}) ⇒ Object
Define a helper for boolean type database stuff Show in a context if wrapped in one of the helpers.
-
.date(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers.
-
.datetime(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers.
-
.expose(name, opts = {}) ⇒ Object
Define a helper for type database stuff Show in a context if wrapped in one of the helpers.
-
.form_props(context = :live) ⇒ Object
Return properties that should be shown for a given context.
-
.front(&block) ⇒ Object
Helper to wrap properties into front level.
-
.front_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does.
-
.fulltext(name, opts = {}) ⇒ Object
Define a helper for fulltext type database stuff Show in a context if wrapped in one of the helpers.
-
.id ⇒ Object
Do setup of object and declare an id.
-
.init ⇒ Object
Stub init method.
-
.orange(&block) ⇒ Object
Helper to wrap properties into orange level.
-
.orange_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does.
-
.scaffold_property(name, type, opts = {}) ⇒ Object
Override DataMapper to include context sensitivity (as set by helpers).
-
.string(name, opts = {}) ⇒ Object
Define a helper for input type=“text” type database stuff Show in a context if wrapped in one of the helpers.
-
.text(name, opts = {}) ⇒ Object
Define a helper for input type=“text” type database stuff Show in a context if wrapped in one of the helpers.
-
.time(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers.
-
.title(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers.
Instance Method Summary collapse
Methods included from ClassInheritableAttributes
cattr_accessor, cattr_reader, cattr_writer, eval_in_accessor_module, fetch_value, store_value
Class Method Details
.add_scaffold(name, type, dm_type, opts) ⇒ Object
81 82 83 84 85 |
# File 'lib/orange-core/carton.rb', line 81 def self.add_scaffold(name, type, dm_type, opts) self.scaffold_properties << {:name => name, :type => type, :levels => @levels}.merge(opts) if @levels || opts.has_key?(:levels) opts = opts.delete_if{|k,v| SCAFFOLD_OPTIONS.include?(k)} # DataMapper doesn't like arbitrary opts self.property(name, dm_type, opts) end |
.admin(&block) ⇒ Object
Helper to wrap properties into admin level
61 62 63 64 65 |
# File 'lib/orange-core/carton.rb', line 61 def self.admin(&block) @levels = [:admin, :orange] instance_eval(&block) @levels = false end |
.admin_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does. The difference is that this will make it an admin property.
150 151 152 153 154 |
# File 'lib/orange-core/carton.rb', line 150 def self.admin_property(name, type, opts = {}) my_type = type.to_s.downcase.to_sym opts[:levels] = [:admin, :orange] add_scaffold(name, my_type, type, opts) end |
.as_resource ⇒ Object
Declares a ModelResource subclass that scaffolds this carton The Subclass will have the name of the carton followed by “_Resource”
31 32 33 34 35 36 37 38 |
# File 'lib/orange-core/carton.rb', line 31 def self.as_resource name = self.to_s eval <<-HEREDOC class ::#{name}_Resource < Orange::ModelResource use #{name} end HEREDOC end |
.boolean(name, opts = {}) ⇒ Object
Define a helper for boolean type database stuff Show in a context if wrapped in one of the helpers
119 120 121 |
# File 'lib/orange-core/carton.rb', line 119 def self.boolean(name, opts = {}) add_scaffold(name, :boolean, Boolean, opts) end |
.date(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers
101 102 103 |
# File 'lib/orange-core/carton.rb', line 101 def self.date(name, opts = {}) add_scaffold(name, :date, Date, opts) end |
.datetime(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers
95 96 97 |
# File 'lib/orange-core/carton.rb', line 95 def self.datetime(name, opts = {}) add_scaffold(name, :datetime, DateTime, opts) end |
.expose(name, opts = {}) ⇒ Object
Define a helper for type database stuff Show in a context if wrapped in one of the helpers
131 132 133 |
# File 'lib/orange-core/carton.rb', line 131 def self.expose(name, opts = {}) self.scaffold_properties << {:name => name, :type => :text, :levels => @levels, :opts => opts} if @levels end |
.form_props(context = :live) ⇒ Object
Return properties that should be shown for a given context
56 57 58 |
# File 'lib/orange-core/carton.rb', line 56 def self.form_props(context = :live) self.scaffold_properties.select{|p| p[:levels].include?(context) } end |
.front(&block) ⇒ Object
Helper to wrap properties into front level
75 76 77 78 79 |
# File 'lib/orange-core/carton.rb', line 75 def self.front(&block) @levels = [:live, :admin, :orange] instance_eval(&block) @levels = false end |
.front_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does. The difference is that this will make it a front property.
158 159 160 161 162 |
# File 'lib/orange-core/carton.rb', line 158 def self.front_property(name, type, opts = {}) my_type = type.to_s.downcase.to_sym opts[:levels] = [:live, :admin, :orange] add_scaffold(name, my_type, type, opts) end |
.fulltext(name, opts = {}) ⇒ Object
Define a helper for fulltext type database stuff Show in a context if wrapped in one of the helpers
113 114 115 |
# File 'lib/orange-core/carton.rb', line 113 def self.fulltext(name, opts = {}) add_scaffold(name, :fulltext, Text, opts) end |
.id ⇒ Object
Do setup of object and declare an id
44 45 46 47 48 49 |
# File 'lib/orange-core/carton.rb', line 44 def self.id include DataMapper::Resource property(:id, Serial) self.scaffold_properties ||= [] self.init end |
.orange(&block) ⇒ Object
Helper to wrap properties into orange level
68 69 70 71 72 |
# File 'lib/orange-core/carton.rb', line 68 def self.orange(&block) @levels = [:orange] instance_eval(&block) @levels = false end |
.orange_property(name, type, opts = {}) ⇒ Object
For more generic cases, use same syntax as DataMapper does. The difference is that this will make it an orange property.
166 167 168 169 170 |
# File 'lib/orange-core/carton.rb', line 166 def self.orange_property(name, type, opts = {}) my_type = type.to_s.downcase.to_sym opts[:levels] = [:orange] add_scaffold(name, my_type, type, opts) end |
.scaffold_property(name, type, opts = {}) ⇒ Object
Override DataMapper to include context sensitivity (as set by helpers)
142 143 144 145 |
# File 'lib/orange-core/carton.rb', line 142 def self.scaffold_property(name, type, opts = {}) my_type = type.to_s.downcase.to_sym add_scaffold(name, my_type, type, opts) end |
.string(name, opts = {}) ⇒ Object
Define a helper for input type=“text” type database stuff Show in a context if wrapped in one of the helpers
137 138 139 |
# File 'lib/orange-core/carton.rb', line 137 def self.string(name, opts = {}) self.text(name, opts) end |
.text(name, opts = {}) ⇒ Object
Define a helper for input type=“text” type database stuff Show in a context if wrapped in one of the helpers
125 126 127 |
# File 'lib/orange-core/carton.rb', line 125 def self.text(name, opts = {}) add_scaffold(name, :text, String, opts) end |
.time(name, opts = {}) ⇒ Object
Define a helper for title type database stuff Show in a context if wrapped in one of the helpers
107 108 109 |
# File 'lib/orange-core/carton.rb', line 107 def self.time(name, opts = {}) add_scaffold(name, :time, Time, opts) end |
Instance Method Details
#to_s ⇒ Object
172 173 174 175 176 |
# File 'lib/orange-core/carton.rb', line 172 def to_s <<-DOC {"class": "#{self.class.to_s}", "id": "#{self.id}"} DOC end |