Module: Tradesman::ClassMethods
- Included in:
- Template
- Defined in:
- lib/tradesman/class_methods.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #adapter ⇒ Object
- #go(obj, params, *context, &block) ⇒ Object
- #go!(obj, params, *context) ⇒ Object
- #id_from_obj(obj) ⇒ Object
- #prepare_ids(obj) ⇒ Object
- #prepare_params(params) ⇒ Object
- #tzu_params(obj, params) ⇒ Object
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
3 4 5 |
# File 'lib/tradesman/class_methods.rb', line 3 def store @store end |
Instance Method Details
#adapter ⇒ Object
5 6 7 |
# File 'lib/tradesman/class_methods.rb', line 5 def adapter Tradesman.adapter.new(store) end |
#go(obj, params, *context, &block) ⇒ Object
9 10 11 |
# File 'lib/tradesman/class_methods.rb', line 9 def go(obj, params, *context, &block) run_and_convert_exceptions { run(tzu_params(obj, params), *context, &block) } end |
#go!(obj, params, *context) ⇒ Object
13 14 15 |
# File 'lib/tradesman/class_methods.rb', line 13 def go!(obj, params, *context) run_and_convert_exceptions { run!(tzu_params(obj, params), *context) } end |
#id_from_obj(obj) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/tradesman/class_methods.rb', line 27 def id_from_obj(obj) id = obj.respond_to?(:id) ? obj.id : obj Integer(id) rescue ArgumentError raise Tradesman::InvalidId.new('You must pass an object that responds to id or an integer') end |
#prepare_ids(obj) ⇒ Object
21 22 23 24 25 |
# File 'lib/tradesman/class_methods.rb', line 21 def prepare_ids(obj) return obj if (obj.is_a?(Hash) && !obj.respond_to?(:id)) return id_from_obj(obj) unless (obj.is_a?(Array) || obj.respond_to?(:push)) obj.map { |object| id_from_obj(object) } end |
#prepare_params(params) ⇒ Object
34 35 36 37 |
# File 'lib/tradesman/class_methods.rb', line 34 def prepare_params(params) return params unless params.is_a? Array { params: params } end |
#tzu_params(obj, params) ⇒ Object
17 18 19 |
# File 'lib/tradesman/class_methods.rb', line 17 def tzu_params(obj, params) { id: prepare_ids(obj) }.merge(prepare_params(params)) end |