Module: Neuron::Client::Base::ClassAndInstanceMethods
- Included in:
- Neuron::Client::Base
- Defined in:
- lib/neuron-client/model/base.rb
Instance Method Summary collapse
- #all ⇒ Object
- #api=(api) ⇒ Object
- #create(attrs = {}) ⇒ Object
- #create!(attrs = {}) ⇒ Object
- #find(id) ⇒ Object
- #validate=(validate) ⇒ Object
Instance Method Details
#all ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/neuron-client/model/base.rb', line 27 def all if connected_to_membase? key = cache_key(":all") connection.local_cache.fetch(key) do cached_json = connection.get(resources_name) if cached_json.present? data = Yajl.load(cached_json) validate_against_schema!(:index, data) data.map{ |hash| from_hash(hash) } else [] end end elsif connected_to_admin? data = connection.get(resources_name) validate_against_schema!(:index, data) data.map{ |hash| from_hash(hash) } else raise "Not configured!" end end |
#api=(api) ⇒ Object
64 65 66 |
# File 'lib/neuron-client/model/base.rb', line 64 def api=(api) @api = api end |
#create(attrs = {}) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/neuron-client/model/base.rb', line 49 def create(attrs={}) @errors = catch :errors do return create!(attrs) end from_hash(resource_name => {'errors' => @errors}) end |
#create!(attrs = {}) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/neuron-client/model/base.rb', line 56 def create!(attrs={}) data = {resource_name => attrs.stringify_keys} validate_against_schema!(:create, data) data = connection.post("#{resources_name}", data) validate_against_schema!(:show, data) from_hash(data) end |
#find(id) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/neuron-client/model/base.rb', line 7 def find(id) if connected_to_membase? key = cache_key(id) connection.local_cache.fetch(key) do cached_json = connection.get(key) if cached_json.present? data = Yajl.load(cached_json) validate_against_schema!(:show, data) from_hash(data) end end elsif connected_to_admin? data = connection.get("#{resources_name}/#{id}") validate_against_schema!(:show, data) from_hash(data) else raise "Not configured!" end end |
#validate=(validate) ⇒ Object
68 69 70 |
# File 'lib/neuron-client/model/base.rb', line 68 def validate=(validate) @validate = validate end |