Module: MundoPepino::ResourcesHistory
- Included in:
- MundoPepino
- Defined in:
- lib/mundo_pepino/resources_history.rb
Defined Under Namespace
Modules: MentionedResource
Instance Method Summary collapse
-
#add_resource(model, attribs = [], options = {}) ⇒ Object
options: :force_creation.
- #add_resource_from_database(raw_model, name) ⇒ Object
- #detect_first(arr, value, method = nil) ⇒ Object
- #last_mentioned ⇒ Object
- #last_mentioned_called(name) ⇒ Object
- #last_mentioned_of(raw_model, with_name = nil) ⇒ Object
- #last_mentioned_resources(model, name) ⇒ Object
- #last_mentioned_url ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #pile_up(mentioned) ⇒ Object
- #recursive_group_search(model, resources) ⇒ Object
- #resources_array_field_and_values(mentioned, campo, valor) ⇒ Object
- #resources_flatten ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/mundo_pepino/resources_history.rb', line 174 def method_missing(method, *args, &block) if (method.to_s =~ /^last_mentioned_(.+)$/) if mentioned = last_mentioned last_mentioned.send("mr_#{$1}") else nil end else super end end |
Instance Method Details
#add_resource(model, attribs = [], options = {}) ⇒ Object
options: :force_creation
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mundo_pepino/resources_history.rb', line 26 def add_resource(model, attribs=[], = {}) attributes = if attribs.is_a?(Hash) [ attribs ] else attribs end res = if attributes.size == 1 find_or_create(model, attributes.first, ) else attributes.map do |hash| find_or_create(model, hash, ) end end pile_up res end |
#add_resource_from_database(raw_model, name) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/mundo_pepino/resources_history.rb', line 42 def add_resource_from_database(raw_model, name) model = convert_to_model(raw_model) field = field_for(model) if resource = model.send("find_by_#{field}", name, :select => :id) pile_up model.find(resource.id) else raise NotFoundInDatabase.new(model, name) end end |
#detect_first(arr, value, method = nil) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/mundo_pepino/resources_history.rb', line 128 def detect_first(arr, value, method = nil) if value.is_a? String method ||= :name arr.detect { |r| r.respond_to?(method) && (r.send(method) =~ /#{value}/i) } elsif value.is_a? Class method ||= :is_a? arr.detect { |r| r.respond_to?(method) && r.send(method, value) } elsif value.is_a? Array model, val = value # [ class, value ] name_field = field_for(model) arr.detect do |r| r.respond_to?(:is_a?) && r.is_a?(model) && r.send(name_field) =~ /#{val}/i end else method ||= :id arr.detect { |r| r.respond_to?(method) && r.send(method) == value } end end |
#last_mentioned ⇒ Object
61 62 63 |
# File 'lib/mundo_pepino/resources_history.rb', line 61 def last_mentioned @resources && @resources.first end |
#last_mentioned_called(name) ⇒ Object
104 105 106 |
# File 'lib/mundo_pepino/resources_history.rb', line 104 def last_mentioned_called(name) detect_first resources_flatten, name end |
#last_mentioned_of(raw_model, with_name = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/mundo_pepino/resources_history.rb', line 77 def last_mentioned_of(raw_model, with_name = nil) if model = raw_model.to_model resource = if with_name if mentioned = detect_first(resources_flatten, [model, with_name]) mentioned else begin add_resource_from_database(raw_model, with_name) rescue raise NotFoundInHistoryNorDatabase.new(model.name, with_name) end end elsif(last_mentioned.mr_model == model) last_mentioned else if group = recursive_group_search(model, @resources[1..-1]) group else detect_first resources_flatten, model end end resource || raise(ResourceNotFound.new("model:#{model.name}, name:#{with_name||'nil'}")) else raise ModelNotMapped.new(raw_model) end end |
#last_mentioned_resources(model, name) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/mundo_pepino/resources_history.rb', line 108 def last_mentioned_resources(model, name) mentioned = last_mentioned_of(model.to_unquoted, name) if mentioned.is_a?(Array) mentioned.each {|m| yield m} else yield mentioned end pile_up mentioned end |
#last_mentioned_url ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mundo_pepino/resources_history.rb', line 65 def last_mentioned_url if mentioned = last_mentioned if mentioned.mr_new_record? eval("#{mentioned.mr_plural}_path") else eval("#{mentioned.mr_singular}_path(mentioned.mr_instance)") end else raise WithoutResources end end |
#pile_up(mentioned) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/mundo_pepino/resources_history.rb', line 52 def pile_up(mentioned) @resources ||= [] if mentioned != last_mentioned mentioned.class.send :include, MentionedResource @resources.unshift mentioned end mentioned end |
#recursive_group_search(model, resources) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/mundo_pepino/resources_history.rb', line 118 def recursive_group_search(model, resources) if lm = resources.shift if(lm.is_a?(Array) and (lm.mr_model == model)) lm else recursive_group_search(model, resources) end end end |
#resources_array_field_and_values(mentioned, campo, valor) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mundo_pepino/resources_history.rb', line 147 def resources_array_field_and_values(mentioned, campo, valor) resources, valores = if mentioned.is_a?(Array) valores = valor.split(/ ?, | y /) if valores.size == mentioned.size [mentioned, valores] else [mentioned, [valor] * mentioned.size] end else [[mentioned], [valor]] end field, values = if (child_model = campo.to_model) child_name_field = field_for(child_model) values = add_resource(child_model, valores.map { |val| { child_name_field => val } }) values = [ values ] unless values.is_a?(Array) [field_for(mentioned.mr_model, campo) || child_model.name.underscore, values] else [field_for(mentioned.mr_model, campo), valores] end [resources, field, values] end |
#resources_flatten ⇒ Object
170 171 172 |
# File 'lib/mundo_pepino/resources_history.rb', line 170 def resources_flatten (@resources || []).flatten end |