logical_model 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +88 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/logical_model.rb +304 -0
- data/logical_model.gemspec +68 -0
- data/test/helper.rb +18 -0
- data/test/test_logical_model.rb +7 -0
- metadata +141 -0
    
        data/.document
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    | @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            # encoding: UTF-8
         | 
| 2 | 
            +
            source "http://rubygems.org"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            # TODO gem "active_model" instead of rails
         | 
| 5 | 
            +
            gem "rails", "~> 3.0.7"
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # TODO gem "yajl-ruby" instead of ActiveResource
         | 
| 8 | 
            +
            gem "typhoeus", "~> 0.2.4"
         | 
| 9 | 
            +
            gem "will_paginate", "~> 3.0.pre2"
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            group :development do
         | 
| 12 | 
            +
              gem "shoulda"
         | 
| 13 | 
            +
              gem "bundler", "~> 1.0.0"
         | 
| 14 | 
            +
              gem "jeweler", "~> 1.6.4"
         | 
| 15 | 
            +
              gem "rcov"
         | 
| 16 | 
            +
            end
         | 
    
        data/Gemfile.lock
    ADDED
    
    | @@ -0,0 +1,88 @@ | |
| 1 | 
            +
            GEM
         | 
| 2 | 
            +
              remote: http://rubygems.org/
         | 
| 3 | 
            +
              specs:
         | 
| 4 | 
            +
                abstract (1.0.0)
         | 
| 5 | 
            +
                actionmailer (3.0.7)
         | 
| 6 | 
            +
                  actionpack (= 3.0.7)
         | 
| 7 | 
            +
                  mail (~> 2.2.15)
         | 
| 8 | 
            +
                actionpack (3.0.7)
         | 
| 9 | 
            +
                  activemodel (= 3.0.7)
         | 
| 10 | 
            +
                  activesupport (= 3.0.7)
         | 
| 11 | 
            +
                  builder (~> 2.1.2)
         | 
| 12 | 
            +
                  erubis (~> 2.6.6)
         | 
| 13 | 
            +
                  i18n (~> 0.5.0)
         | 
| 14 | 
            +
                  rack (~> 1.2.1)
         | 
| 15 | 
            +
                  rack-mount (~> 0.6.14)
         | 
| 16 | 
            +
                  rack-test (~> 0.5.7)
         | 
| 17 | 
            +
                  tzinfo (~> 0.3.23)
         | 
| 18 | 
            +
                activemodel (3.0.7)
         | 
| 19 | 
            +
                  activesupport (= 3.0.7)
         | 
| 20 | 
            +
                  builder (~> 2.1.2)
         | 
| 21 | 
            +
                  i18n (~> 0.5.0)
         | 
| 22 | 
            +
                activerecord (3.0.7)
         | 
| 23 | 
            +
                  activemodel (= 3.0.7)
         | 
| 24 | 
            +
                  activesupport (= 3.0.7)
         | 
| 25 | 
            +
                  arel (~> 2.0.2)
         | 
| 26 | 
            +
                  tzinfo (~> 0.3.23)
         | 
| 27 | 
            +
                activeresource (3.0.7)
         | 
| 28 | 
            +
                  activemodel (= 3.0.7)
         | 
| 29 | 
            +
                  activesupport (= 3.0.7)
         | 
| 30 | 
            +
                activesupport (3.0.7)
         | 
| 31 | 
            +
                arel (2.0.10)
         | 
| 32 | 
            +
                builder (2.1.2)
         | 
| 33 | 
            +
                erubis (2.6.6)
         | 
| 34 | 
            +
                  abstract (>= 1.0.0)
         | 
| 35 | 
            +
                git (1.2.5)
         | 
| 36 | 
            +
                i18n (0.5.0)
         | 
| 37 | 
            +
                jeweler (1.6.4)
         | 
| 38 | 
            +
                  bundler (~> 1.0)
         | 
| 39 | 
            +
                  git (>= 1.2.5)
         | 
| 40 | 
            +
                  rake
         | 
| 41 | 
            +
                mail (2.2.19)
         | 
| 42 | 
            +
                  activesupport (>= 2.3.6)
         | 
| 43 | 
            +
                  i18n (>= 0.4.0)
         | 
| 44 | 
            +
                  mime-types (~> 1.16)
         | 
| 45 | 
            +
                  treetop (~> 1.4.8)
         | 
| 46 | 
            +
                mime-types (1.16)
         | 
| 47 | 
            +
                polyglot (0.3.1)
         | 
| 48 | 
            +
                rack (1.2.3)
         | 
| 49 | 
            +
                rack-mount (0.6.14)
         | 
| 50 | 
            +
                  rack (>= 1.0.0)
         | 
| 51 | 
            +
                rack-test (0.5.7)
         | 
| 52 | 
            +
                  rack (>= 1.0)
         | 
| 53 | 
            +
                rails (3.0.7)
         | 
| 54 | 
            +
                  actionmailer (= 3.0.7)
         | 
| 55 | 
            +
                  actionpack (= 3.0.7)
         | 
| 56 | 
            +
                  activerecord (= 3.0.7)
         | 
| 57 | 
            +
                  activeresource (= 3.0.7)
         | 
| 58 | 
            +
                  activesupport (= 3.0.7)
         | 
| 59 | 
            +
                  bundler (~> 1.0)
         | 
| 60 | 
            +
                  railties (= 3.0.7)
         | 
| 61 | 
            +
                railties (3.0.7)
         | 
| 62 | 
            +
                  actionpack (= 3.0.7)
         | 
| 63 | 
            +
                  activesupport (= 3.0.7)
         | 
| 64 | 
            +
                  rake (>= 0.8.7)
         | 
| 65 | 
            +
                  thor (~> 0.14.4)
         | 
| 66 | 
            +
                rake (0.9.2)
         | 
| 67 | 
            +
                rcov (0.9.9)
         | 
| 68 | 
            +
                shoulda (2.11.3)
         | 
| 69 | 
            +
                thor (0.14.6)
         | 
| 70 | 
            +
                treetop (1.4.9)
         | 
| 71 | 
            +
                  polyglot (>= 0.3.1)
         | 
| 72 | 
            +
                typhoeus (0.2.4)
         | 
| 73 | 
            +
                  mime-types
         | 
| 74 | 
            +
                  mime-types
         | 
| 75 | 
            +
                tzinfo (0.3.29)
         | 
| 76 | 
            +
                will_paginate (3.0.pre2)
         | 
| 77 | 
            +
             | 
| 78 | 
            +
            PLATFORMS
         | 
| 79 | 
            +
              ruby
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            DEPENDENCIES
         | 
| 82 | 
            +
              bundler (~> 1.0.0)
         | 
| 83 | 
            +
              jeweler (~> 1.6.4)
         | 
| 84 | 
            +
              rails (~> 3.0.7)
         | 
| 85 | 
            +
              rcov
         | 
| 86 | 
            +
              shoulda
         | 
| 87 | 
            +
              typhoeus (~> 0.2.4)
         | 
| 88 | 
            +
              will_paginate (~> 3.0.pre2)
         | 
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            Copyright (c) 2011 Dwayne Macgowan
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 4 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 5 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 6 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 7 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 8 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 9 | 
            +
            the following conditions:
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 12 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 15 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 16 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 17 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 18 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 19 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 20 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.rdoc
    ADDED
    
    | @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            = logical_model
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            LogicalModel allows to use a restfull resources as models.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            It was written following this tutorial: http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            == Contributing to logical_model
         | 
| 8 | 
            +
             
         | 
| 9 | 
            +
            * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
         | 
| 10 | 
            +
            * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
         | 
| 11 | 
            +
            * Fork the project
         | 
| 12 | 
            +
            * Start a feature/bugfix branch
         | 
| 13 | 
            +
            * Commit and push until you are happy with your contribution
         | 
| 14 | 
            +
            * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
         | 
| 15 | 
            +
            * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            == Copyright
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            Copyright (c) 2011 Dwayne Macgowan. See LICENSE.txt for
         | 
| 20 | 
            +
            further details.
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            === TODO
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            * specs!
         | 
| 25 | 
            +
            * remove rails dependency
         | 
| 26 | 
            +
            * put log in /log
         | 
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,53 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'rubygems'
         | 
| 4 | 
            +
            require 'bundler'
         | 
| 5 | 
            +
            begin
         | 
| 6 | 
            +
              Bundler.setup(:default, :development)
         | 
| 7 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 8 | 
            +
              $stderr.puts e.message
         | 
| 9 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 10 | 
            +
              exit e.status_code
         | 
| 11 | 
            +
            end
         | 
| 12 | 
            +
            require 'rake'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            require 'jeweler'
         | 
| 15 | 
            +
            Jeweler::Tasks.new do |gem|
         | 
| 16 | 
            +
              # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
         | 
| 17 | 
            +
              gem.name = "logical_model"
         | 
| 18 | 
            +
              gem.homepage = "http://github.com/dwaynemac/logical_model"
         | 
| 19 | 
            +
              gem.license = "MIT"
         | 
| 20 | 
            +
              gem.summary = %Q{LogicalModel allows to use a resource as a model. }
         | 
| 21 | 
            +
              gem.description = %Q{LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3}
         | 
| 22 | 
            +
              gem.email = "dwaynemac@gmail.com"
         | 
| 23 | 
            +
              gem.authors = ["Dwayne Macgowan"]
         | 
| 24 | 
            +
              # dependencies defined in Gemfile
         | 
| 25 | 
            +
            end
         | 
| 26 | 
            +
            Jeweler::RubygemsDotOrgTasks.new
         | 
| 27 | 
            +
             | 
| 28 | 
            +
            require 'rake/testtask'
         | 
| 29 | 
            +
            Rake::TestTask.new(:test) do |test|
         | 
| 30 | 
            +
              test.libs << 'lib' << 'test'
         | 
| 31 | 
            +
              test.pattern = 'test/**/test_*.rb'
         | 
| 32 | 
            +
              test.verbose = true
         | 
| 33 | 
            +
            end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            require 'rcov/rcovtask'
         | 
| 36 | 
            +
            Rcov::RcovTask.new do |test|
         | 
| 37 | 
            +
              test.libs << 'test'
         | 
| 38 | 
            +
              test.pattern = 'test/**/test_*.rb'
         | 
| 39 | 
            +
              test.verbose = true
         | 
| 40 | 
            +
              test.rcov_opts << '--exclude "gems/*"'
         | 
| 41 | 
            +
            end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            task :default => :test
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            require 'rake/rdoctask'
         | 
| 46 | 
            +
            Rake::RDocTask.new do |rdoc|
         | 
| 47 | 
            +
              version = File.exist?('VERSION') ? File.read('VERSION') : ""
         | 
| 48 | 
            +
             | 
| 49 | 
            +
              rdoc.rdoc_dir = 'rdoc'
         | 
| 50 | 
            +
              rdoc.title = "logical_model #{version}"
         | 
| 51 | 
            +
              rdoc.rdoc_files.include('README*')
         | 
| 52 | 
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         | 
| 53 | 
            +
            end
         | 
    
        data/VERSION
    ADDED
    
    | @@ -0,0 +1 @@ | |
| 1 | 
            +
            0.1.4
         | 
| @@ -0,0 +1,304 @@ | |
| 1 | 
            +
            require 'active_model'
         | 
| 2 | 
            +
            require 'typhoeus'
         | 
| 3 | 
            +
            require 'active_support' # todo migrate to yajl
         | 
| 4 | 
            +
            require 'logger'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            # Logical Model, not persistant on DB, works through API. (replaces ActiveResource)
         | 
| 7 | 
            +
            #
         | 
| 8 | 
            +
            #
         | 
| 9 | 
            +
            # Configuration attributes:
         | 
| 10 | 
            +
            #   host: Host of the WS. eg: "localhost:3000"
         | 
| 11 | 
            +
            #   resource_path: Path of this resources. eg: "/api/resources"
         | 
| 12 | 
            +
            #   attribute_keys: Attributes. eg: [:id, :attr_a, :attr_b]
         | 
| 13 | 
            +
            #   use_ssl: will use https if true, http if false
         | 
| 14 | 
            +
            #   use_api_key: set to true if api_key is needed to access resource
         | 
| 15 | 
            +
            #   api_key_name: api key parameter name. eg: app_key
         | 
| 16 | 
            +
            #   api_key: api_key. eg: "asd32fa4s4pdf35tr"
         | 
| 17 | 
            +
            #   log_path: Path to log file. Will be ignored if using Rails.
         | 
| 18 | 
            +
            #
         | 
| 19 | 
            +
            # You may use validations such as validates_presence_of, etc.
         | 
| 20 | 
            +
            #
         | 
| 21 | 
            +
            # Usage:
         | 
| 22 | 
            +
            #  class RemoteResource < LogicalModel
         | 
| 23 | 
            +
            #    self.host = "http://remote.server"
         | 
| 24 | 
            +
            #    self.resource_path = "/api/remote/path"
         | 
| 25 | 
            +
            #    self.attribute_keys = [:id, :attribute_a, :attribute_b]
         | 
| 26 | 
            +
            #
         | 
| 27 | 
            +
            #    validates_presence_of :id
         | 
| 28 | 
            +
            #  end
         | 
| 29 | 
            +
            #
         | 
| 30 | 
            +
            #  This enables:
         | 
| 31 | 
            +
            #
         | 
| 32 | 
            +
            #  RemoteResource.new(params[:remote_resource])
         | 
| 33 | 
            +
            #  RemoteResource#create
         | 
| 34 | 
            +
            #  RemoteResource.find(params[:id])
         | 
| 35 | 
            +
            #  RemoteResource.paginate
         | 
| 36 | 
            +
            #  RemoteResource#update(params[:remote_resouce])
         | 
| 37 | 
            +
            #  RemoteResource.delete(params[:id])
         | 
| 38 | 
            +
            #  RemoteResource#destroy
         | 
| 39 | 
            +
            class LogicalModel
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              # include ActiveModel Modules that are usefull
         | 
| 42 | 
            +
              extend ActiveModel::Naming
         | 
| 43 | 
            +
              include ActiveModel::Conversion
         | 
| 44 | 
            +
              include ActiveModel::Serializers::JSON
         | 
| 45 | 
            +
              include ActiveModel::Validations
         | 
| 46 | 
            +
              include ActiveModel::MassAssignmentSecurity
         | 
| 47 | 
            +
             | 
| 48 | 
            +
              self.include_root_in_json = false
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def self.attribute_keys=(keys)
         | 
| 51 | 
            +
                @attribute_keys = keys
         | 
| 52 | 
            +
                attr_accessor *keys
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              def self.attribute_keys
         | 
| 56 | 
            +
                @attribute_keys
         | 
| 57 | 
            +
              end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
              class << self
         | 
| 60 | 
            +
                attr_accessor :host, :hydra, :resource_path, :use_ssl, :use_api_key, :api_key, :api_key_name, :log_path
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                # host eg: "127.0.0.1:3010"
         | 
| 63 | 
            +
                # resource_path eg: "/api/v1/people"
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              def self.resource_uri(id=nil)
         | 
| 67 | 
            +
                prefix = (use_ssl)? "https://" : "http://"
         | 
| 68 | 
            +
                sufix  = (id.nil?)? "" : "/#{id}"
         | 
| 69 | 
            +
                "#{prefix}#{host}#{resource_path}#{sufix}"
         | 
| 70 | 
            +
              end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
              def persisted?
         | 
| 73 | 
            +
                false
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              def initialize(attributes={})
         | 
| 77 | 
            +
                self.attributes = attributes
         | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
              def attributes
         | 
| 81 | 
            +
                self.class.attribute_keys.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result,key|
         | 
| 82 | 
            +
                  result[key] = read_attribute_for_validation(key)
         | 
| 83 | 
            +
                  result
         | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
              end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
              def attributes=(attrs)
         | 
| 88 | 
            +
                sanitize_for_mass_assignment(attrs).each{|k,v| send("#{k}=",v) if respond_to?("#{k}=")}
         | 
| 89 | 
            +
              end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
             | 
| 92 | 
            +
              def self.from_json(json_string)
         | 
| 93 | 
            +
                parsed = ActiveSupport::JSON.decode(json_string)
         | 
| 94 | 
            +
                collection = parsed["collection"].map{|i|self.new(i)}
         | 
| 95 | 
            +
                return { :collection => collection, :total => parsed["total"].to_i }
         | 
| 96 | 
            +
              end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
              def self.log_ok(response)
         | 
| 99 | 
            +
                self.logger.info("LogicalModel Log: #{response.code} #{response.request.url} in #{response.time}s")
         | 
| 100 | 
            +
                self.logger.debug("LogicalModel Log RESPONSE: #{response.body}")
         | 
| 101 | 
            +
              end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
              def log_ok(response)
         | 
| 104 | 
            +
                self.class.log_ok(response)
         | 
| 105 | 
            +
              end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
              def self.log_failed(response)
         | 
| 108 | 
            +
                # error_message = ActiveSupport::JSON.decode(response.body)["message"]
         | 
| 109 | 
            +
                error_message = "error"
         | 
| 110 | 
            +
                msg = "LogicalModel Log: #{response.code} #{response.request.url} in #{response.time}s FAILED: #{error_message}"
         | 
| 111 | 
            +
                self.logger.warn(msg)
         | 
| 112 | 
            +
                self.logger.debug("LogicalModel Log RESPONSE: #{response.body}")
         | 
| 113 | 
            +
              end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
              def log_failed(response)
         | 
| 116 | 
            +
                self.class.log_failed(response)
         | 
| 117 | 
            +
              end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
              def self.logger
         | 
| 120 | 
            +
                if defined?(Rails)
         | 
| 121 | 
            +
                  Rails.logger
         | 
| 122 | 
            +
                else
         | 
| 123 | 
            +
                  path = self.log_path.nil?? "log.log" : self.log_path
         | 
| 124 | 
            +
                  Logger.new(path)
         | 
| 125 | 
            +
                end
         | 
| 126 | 
            +
              end
         | 
| 127 | 
            +
             | 
| 128 | 
            +
              # if needed willmerge api_key into given hash
         | 
| 129 | 
            +
              # returns merged hash
         | 
| 130 | 
            +
              def self.merge_key(params = {})
         | 
| 131 | 
            +
                if self.use_api_key
         | 
| 132 | 
            +
                  params.merge({self.api_key_name => self.api_key})
         | 
| 133 | 
            +
                else
         | 
| 134 | 
            +
                  params
         | 
| 135 | 
            +
                end
         | 
| 136 | 
            +
              end
         | 
| 137 | 
            +
             | 
| 138 | 
            +
              #  ============================================================================================================
         | 
| 139 | 
            +
              #  Following methods are API specific.
         | 
| 140 | 
            +
              #  They assume we are using a RESTfull API.
         | 
| 141 | 
            +
              #  for get, put, delete :id is expected
         | 
| 142 | 
            +
              #  for post, put attributes are excepted under class_name directly. eg. put( {:id => 1, :class_name => {:attr => "new value for attr"}} )
         | 
| 143 | 
            +
              #  ============================================================================================================
         | 
| 144 | 
            +
             | 
| 145 | 
            +
              # Asynchronic Pagination
         | 
| 146 | 
            +
              #  This pagination won't block excecution waiting for result, pagination will be enqueued in Objectr#hydra.
         | 
| 147 | 
            +
              #
         | 
| 148 | 
            +
              # Parameters:
         | 
| 149 | 
            +
              #   - options hash.
         | 
| 150 | 
            +
              #   Valid options are:
         | 
| 151 | 
            +
              #   * :page - indicated what page to return. Defaults to 1.
         | 
| 152 | 
            +
              #   * :per_page - indicates how many records to be returned per page. Defauls to 20
         | 
| 153 | 
            +
              #   * all other options will be sent in :params to WebService
         | 
| 154 | 
            +
              #
         | 
| 155 | 
            +
              # Usage:
         | 
| 156 | 
            +
              #   Person.async_paginate(:page => params[:page]){|i| result = i}
         | 
| 157 | 
            +
              def self.async_paginate(options={})
         | 
| 158 | 
            +
                options[:page] ||= 1
         | 
| 159 | 
            +
                options[:per_page] ||= 20
         | 
| 160 | 
            +
             | 
| 161 | 
            +
                options = self.merge_key(options)
         | 
| 162 | 
            +
             | 
| 163 | 
            +
                request = Typhoeus::Request.new(resource_uri, :params => options)
         | 
| 164 | 
            +
                request.on_complete do |response|
         | 
| 165 | 
            +
                  if response.code >= 200 && response.code < 400
         | 
| 166 | 
            +
                    log_ok(response)
         | 
| 167 | 
            +
             | 
| 168 | 
            +
                    result_set = self.from_json(response.body)
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                    # this paginate is will_paginate's Array pagination
         | 
| 171 | 
            +
                    collection = result_set[:collection].paginate(:page => options[:page],
         | 
| 172 | 
            +
                                                                  :total_entries => result_set[:total],
         | 
| 173 | 
            +
                                                                  :per_page => options[:per_page])
         | 
| 174 | 
            +
             | 
| 175 | 
            +
                    yield collection
         | 
| 176 | 
            +
                  else
         | 
| 177 | 
            +
                    log_failed(response)
         | 
| 178 | 
            +
                  end
         | 
| 179 | 
            +
                end
         | 
| 180 | 
            +
                self.hydra.queue(request)
         | 
| 181 | 
            +
              end
         | 
| 182 | 
            +
             | 
| 183 | 
            +
              #synchronic pagination
         | 
| 184 | 
            +
              def self.paginate(options={})
         | 
| 185 | 
            +
                result = nil
         | 
| 186 | 
            +
                async_paginate(options){|i| result = i}
         | 
| 187 | 
            +
                self.hydra.run
         | 
| 188 | 
            +
                result
         | 
| 189 | 
            +
              end
         | 
| 190 | 
            +
             | 
| 191 | 
            +
              # Asynchronic Find
         | 
| 192 | 
            +
              #  This find won't block excecution waiting for result, excecution will be enqueued in Objectr#hydra.
         | 
| 193 | 
            +
              #
         | 
| 194 | 
            +
              # Parameters:
         | 
| 195 | 
            +
              #   - id, id of object to find
         | 
| 196 | 
            +
              #
         | 
| 197 | 
            +
              # Usage:
         | 
| 198 | 
            +
              #   Person.async_find(params[:id])
         | 
| 199 | 
            +
              def self.async_find(id)
         | 
| 200 | 
            +
                params = self.merge_key
         | 
| 201 | 
            +
                request = Typhoeus::Request.new( resource_uri(id), :params => params )
         | 
| 202 | 
            +
             | 
| 203 | 
            +
                request.on_complete do |response|
         | 
| 204 | 
            +
                  if response.code >= 200 && response.code < 400
         | 
| 205 | 
            +
                    log_ok(response)
         | 
| 206 | 
            +
                    yield self.new.from_json(response.body) # this from_json is defined in ActiveModel::Serializers::JSON
         | 
| 207 | 
            +
                  else
         | 
| 208 | 
            +
                    log_failed(response)
         | 
| 209 | 
            +
                  end
         | 
| 210 | 
            +
                end
         | 
| 211 | 
            +
             | 
| 212 | 
            +
                self.hydra.queue(request)
         | 
| 213 | 
            +
              end
         | 
| 214 | 
            +
             | 
| 215 | 
            +
              # synchronic find
         | 
| 216 | 
            +
              def self.find(id)
         | 
| 217 | 
            +
                result = nil
         | 
| 218 | 
            +
                async_find(id){|i| result = i}
         | 
| 219 | 
            +
                self.hydra.run
         | 
| 220 | 
            +
                result
         | 
| 221 | 
            +
              end
         | 
| 222 | 
            +
             | 
| 223 | 
            +
              #
         | 
| 224 | 
            +
              # creates model.
         | 
| 225 | 
            +
              #
         | 
| 226 | 
            +
              # returns:
         | 
| 227 | 
            +
              #   - false if model invalid
         | 
| 228 | 
            +
              #   - nil if there was a connection problem
         | 
| 229 | 
            +
              #   - created model ID if successfull
         | 
| 230 | 
            +
              #
         | 
| 231 | 
            +
              # Usage:
         | 
| 232 | 
            +
              #   @person = Person.new(parmas[:person])
         | 
| 233 | 
            +
              #   @person.create
         | 
| 234 | 
            +
              def create
         | 
| 235 | 
            +
                return false unless valid?
         | 
| 236 | 
            +
             | 
| 237 | 
            +
                params = self.attributes
         | 
| 238 | 
            +
                params = self.merge_key(params)
         | 
| 239 | 
            +
             | 
| 240 | 
            +
                response = Typhoeus::Request.post( self.resource_uri, :params => params )
         | 
| 241 | 
            +
                if response.code == 201
         | 
| 242 | 
            +
                  log_ok(response)
         | 
| 243 | 
            +
                  self.id = ActiveSupport::JSON.decode(response.body)["id"]
         | 
| 244 | 
            +
                else
         | 
| 245 | 
            +
                  log_failed(response)
         | 
| 246 | 
            +
                  return nil
         | 
| 247 | 
            +
                end
         | 
| 248 | 
            +
              end
         | 
| 249 | 
            +
             | 
| 250 | 
            +
              # Updates Objects attributes.
         | 
| 251 | 
            +
              #
         | 
| 252 | 
            +
              # Returns false if Object#valid? is false.
         | 
| 253 | 
            +
              # Returns updated object if successfull.
         | 
| 254 | 
            +
              # Returns nil if update failed
         | 
| 255 | 
            +
              #
         | 
| 256 | 
            +
              # Usage:
         | 
| 257 | 
            +
              #   @person.update(params[:person])
         | 
| 258 | 
            +
              def update(attributes)
         | 
| 259 | 
            +
             | 
| 260 | 
            +
                self.attributes = attributes
         | 
| 261 | 
            +
             | 
| 262 | 
            +
                return false unless valid?
         | 
| 263 | 
            +
             | 
| 264 | 
            +
                params = { :self.class.underscore => self.attributes }
         | 
| 265 | 
            +
                params = self.merge_key(params)
         | 
| 266 | 
            +
                response = Typhoeus::Request.put( self.resource_uri(id), :params => params )
         | 
| 267 | 
            +
                if response.code == 200
         | 
| 268 | 
            +
                  log_ok(response)
         | 
| 269 | 
            +
                  return self
         | 
| 270 | 
            +
                else
         | 
| 271 | 
            +
                  log_failed(response)
         | 
| 272 | 
            +
                  return nil
         | 
| 273 | 
            +
                end
         | 
| 274 | 
            +
              end
         | 
| 275 | 
            +
             | 
| 276 | 
            +
              # Deletes Object#id
         | 
| 277 | 
            +
              #
         | 
| 278 | 
            +
              # Returns nil if delete failed
         | 
| 279 | 
            +
              #
         | 
| 280 | 
            +
              # Usage:
         | 
| 281 | 
            +
              #   Person.delete(params[:id])
         | 
| 282 | 
            +
              def self.delete(id)
         | 
| 283 | 
            +
             | 
| 284 | 
            +
                params = self.merge_key
         | 
| 285 | 
            +
             | 
| 286 | 
            +
                response = Typhoeus::Request.delete( self.resource_uri(id), :params => params )
         | 
| 287 | 
            +
                if response == 200
         | 
| 288 | 
            +
                  log_ok(response)
         | 
| 289 | 
            +
                  return self
         | 
| 290 | 
            +
                else
         | 
| 291 | 
            +
                  log_failed(response)
         | 
| 292 | 
            +
                  return nil
         | 
| 293 | 
            +
                end
         | 
| 294 | 
            +
              end
         | 
| 295 | 
            +
             | 
| 296 | 
            +
              # Destroy object
         | 
| 297 | 
            +
              #
         | 
| 298 | 
            +
              # Usage:
         | 
| 299 | 
            +
              #   @person.destroy
         | 
| 300 | 
            +
              def destroy
         | 
| 301 | 
            +
                self.class.delete(self.id)
         | 
| 302 | 
            +
              end
         | 
| 303 | 
            +
             | 
| 304 | 
            +
            end
         | 
| @@ -0,0 +1,68 @@ | |
| 1 | 
            +
            # Generated by jeweler
         | 
| 2 | 
            +
            # DO NOT EDIT THIS FILE DIRECTLY
         | 
| 3 | 
            +
            # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
         | 
| 4 | 
            +
            # -*- encoding: utf-8 -*-
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |s|
         | 
| 7 | 
            +
              s.name = %q{logical_model}
         | 
| 8 | 
            +
              s.version = "0.1.4"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         | 
| 11 | 
            +
              s.authors = ["Dwayne Macgowan"]
         | 
| 12 | 
            +
              s.date = %q{2011-07-25}
         | 
| 13 | 
            +
              s.description = %q{LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3}
         | 
| 14 | 
            +
              s.email = %q{dwaynemac@gmail.com}
         | 
| 15 | 
            +
              s.extra_rdoc_files = [
         | 
| 16 | 
            +
                "LICENSE.txt",
         | 
| 17 | 
            +
                "README.rdoc"
         | 
| 18 | 
            +
              ]
         | 
| 19 | 
            +
              s.files = [
         | 
| 20 | 
            +
                ".document",
         | 
| 21 | 
            +
                "Gemfile",
         | 
| 22 | 
            +
                "Gemfile.lock",
         | 
| 23 | 
            +
                "LICENSE.txt",
         | 
| 24 | 
            +
                "README.rdoc",
         | 
| 25 | 
            +
                "Rakefile",
         | 
| 26 | 
            +
                "VERSION",
         | 
| 27 | 
            +
                "lib/logical_model.rb",
         | 
| 28 | 
            +
                "logical_model.gemspec",
         | 
| 29 | 
            +
                "test/helper.rb",
         | 
| 30 | 
            +
                "test/test_logical_model.rb"
         | 
| 31 | 
            +
              ]
         | 
| 32 | 
            +
              s.homepage = %q{http://github.com/dwaynemac/logical_model}
         | 
| 33 | 
            +
              s.licenses = ["MIT"]
         | 
| 34 | 
            +
              s.require_paths = ["lib"]
         | 
| 35 | 
            +
              s.rubygems_version = %q{1.5.0}
         | 
| 36 | 
            +
              s.summary = %q{LogicalModel allows to use a resource as a model.}
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              if s.respond_to? :specification_version then
         | 
| 39 | 
            +
                s.specification_version = 3
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
         | 
| 42 | 
            +
                  s.add_runtime_dependency(%q<rails>, ["~> 3.0.7"])
         | 
| 43 | 
            +
                  s.add_runtime_dependency(%q<typhoeus>, ["~> 0.2.4"])
         | 
| 44 | 
            +
                  s.add_runtime_dependency(%q<will_paginate>, ["~> 3.0.pre2"])
         | 
| 45 | 
            +
                  s.add_development_dependency(%q<shoulda>, [">= 0"])
         | 
| 46 | 
            +
                  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 47 | 
            +
                  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 48 | 
            +
                  s.add_development_dependency(%q<rcov>, [">= 0"])
         | 
| 49 | 
            +
                else
         | 
| 50 | 
            +
                  s.add_dependency(%q<rails>, ["~> 3.0.7"])
         | 
| 51 | 
            +
                  s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
         | 
| 52 | 
            +
                  s.add_dependency(%q<will_paginate>, ["~> 3.0.pre2"])
         | 
| 53 | 
            +
                  s.add_dependency(%q<shoulda>, [">= 0"])
         | 
| 54 | 
            +
                  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 55 | 
            +
                  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 56 | 
            +
                  s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
              else
         | 
| 59 | 
            +
                s.add_dependency(%q<rails>, ["~> 3.0.7"])
         | 
| 60 | 
            +
                s.add_dependency(%q<typhoeus>, ["~> 0.2.4"])
         | 
| 61 | 
            +
                s.add_dependency(%q<will_paginate>, ["~> 3.0.pre2"])
         | 
| 62 | 
            +
                s.add_dependency(%q<shoulda>, [">= 0"])
         | 
| 63 | 
            +
                s.add_dependency(%q<bundler>, ["~> 1.0.0"])
         | 
| 64 | 
            +
                s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
         | 
| 65 | 
            +
                s.add_dependency(%q<rcov>, [">= 0"])
         | 
| 66 | 
            +
              end
         | 
| 67 | 
            +
            end
         | 
| 68 | 
            +
             | 
    
        data/test/helper.rb
    ADDED
    
    | @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            require 'rubygems'
         | 
| 2 | 
            +
            require 'bundler'
         | 
| 3 | 
            +
            begin
         | 
| 4 | 
            +
              Bundler.setup(:default, :development)
         | 
| 5 | 
            +
            rescue Bundler::BundlerError => e
         | 
| 6 | 
            +
              $stderr.puts e.message
         | 
| 7 | 
            +
              $stderr.puts "Run `bundle install` to install missing gems"
         | 
| 8 | 
            +
              exit e.status_code
         | 
| 9 | 
            +
            end
         | 
| 10 | 
            +
            require 'test/unit'
         | 
| 11 | 
            +
            require 'shoulda'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
         | 
| 14 | 
            +
            $LOAD_PATH.unshift(File.dirname(__FILE__))
         | 
| 15 | 
            +
            require 'logical_model'
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            class Test::Unit::TestCase
         | 
| 18 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,141 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: logical_model
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.4
         | 
| 5 | 
            +
              prerelease: !!null 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Dwayne Macgowan
         | 
| 9 | 
            +
            autorequire: !!null 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2011-07-25 00:00:00.000000000 -03:00
         | 
| 13 | 
            +
            default_executable: !!null 
         | 
| 14 | 
            +
            dependencies:
         | 
| 15 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 16 | 
            +
              name: rails
         | 
| 17 | 
            +
              requirement: &70018750 !ruby/object:Gem::Requirement
         | 
| 18 | 
            +
                none: false
         | 
| 19 | 
            +
                requirements:
         | 
| 20 | 
            +
                - - ~>
         | 
| 21 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            +
                    version: 3.0.7
         | 
| 23 | 
            +
              type: :runtime
         | 
| 24 | 
            +
              prerelease: false
         | 
| 25 | 
            +
              version_requirements: *70018750
         | 
| 26 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 27 | 
            +
              name: typhoeus
         | 
| 28 | 
            +
              requirement: &70017580 !ruby/object:Gem::Requirement
         | 
| 29 | 
            +
                none: false
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ~>
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: 0.2.4
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: *70017580
         | 
| 37 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 38 | 
            +
              name: will_paginate
         | 
| 39 | 
            +
              requirement: &70016420 !ruby/object:Gem::Requirement
         | 
| 40 | 
            +
                none: false
         | 
| 41 | 
            +
                requirements:
         | 
| 42 | 
            +
                - - ~>
         | 
| 43 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 44 | 
            +
                    version: 3.0.pre2
         | 
| 45 | 
            +
              type: :runtime
         | 
| 46 | 
            +
              prerelease: false
         | 
| 47 | 
            +
              version_requirements: *70016420
         | 
| 48 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 49 | 
            +
              name: shoulda
         | 
| 50 | 
            +
              requirement: &70015750 !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                none: false
         | 
| 52 | 
            +
                requirements:
         | 
| 53 | 
            +
                - - ! '>='
         | 
| 54 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 55 | 
            +
                    version: '0'
         | 
| 56 | 
            +
              type: :development
         | 
| 57 | 
            +
              prerelease: false
         | 
| 58 | 
            +
              version_requirements: *70015750
         | 
| 59 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 60 | 
            +
              name: bundler
         | 
| 61 | 
            +
              requirement: &70015010 !ruby/object:Gem::Requirement
         | 
| 62 | 
            +
                none: false
         | 
| 63 | 
            +
                requirements:
         | 
| 64 | 
            +
                - - ~>
         | 
| 65 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 66 | 
            +
                    version: 1.0.0
         | 
| 67 | 
            +
              type: :development
         | 
| 68 | 
            +
              prerelease: false
         | 
| 69 | 
            +
              version_requirements: *70015010
         | 
| 70 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 71 | 
            +
              name: jeweler
         | 
| 72 | 
            +
              requirement: &70014340 !ruby/object:Gem::Requirement
         | 
| 73 | 
            +
                none: false
         | 
| 74 | 
            +
                requirements:
         | 
| 75 | 
            +
                - - ~>
         | 
| 76 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 77 | 
            +
                    version: 1.6.4
         | 
| 78 | 
            +
              type: :development
         | 
| 79 | 
            +
              prerelease: false
         | 
| 80 | 
            +
              version_requirements: *70014340
         | 
| 81 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 82 | 
            +
              name: rcov
         | 
| 83 | 
            +
              requirement: &70013990 !ruby/object:Gem::Requirement
         | 
| 84 | 
            +
                none: false
         | 
| 85 | 
            +
                requirements:
         | 
| 86 | 
            +
                - - ! '>='
         | 
| 87 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 88 | 
            +
                    version: '0'
         | 
| 89 | 
            +
              type: :development
         | 
| 90 | 
            +
              prerelease: false
         | 
| 91 | 
            +
              version_requirements: *70013990
         | 
| 92 | 
            +
            description: LogicalModel allows to use a resource as a model. It is based on web
         | 
| 93 | 
            +
              presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3
         | 
| 94 | 
            +
            email: dwaynemac@gmail.com
         | 
| 95 | 
            +
            executables: []
         | 
| 96 | 
            +
            extensions: []
         | 
| 97 | 
            +
            extra_rdoc_files:
         | 
| 98 | 
            +
            - LICENSE.txt
         | 
| 99 | 
            +
            - README.rdoc
         | 
| 100 | 
            +
            files:
         | 
| 101 | 
            +
            - .document
         | 
| 102 | 
            +
            - Gemfile
         | 
| 103 | 
            +
            - Gemfile.lock
         | 
| 104 | 
            +
            - LICENSE.txt
         | 
| 105 | 
            +
            - README.rdoc
         | 
| 106 | 
            +
            - Rakefile
         | 
| 107 | 
            +
            - VERSION
         | 
| 108 | 
            +
            - lib/logical_model.rb
         | 
| 109 | 
            +
            - logical_model.gemspec
         | 
| 110 | 
            +
            - test/helper.rb
         | 
| 111 | 
            +
            - test/test_logical_model.rb
         | 
| 112 | 
            +
            has_rdoc: true
         | 
| 113 | 
            +
            homepage: http://github.com/dwaynemac/logical_model
         | 
| 114 | 
            +
            licenses:
         | 
| 115 | 
            +
            - MIT
         | 
| 116 | 
            +
            post_install_message: !!null 
         | 
| 117 | 
            +
            rdoc_options: []
         | 
| 118 | 
            +
            require_paths:
         | 
| 119 | 
            +
            - lib
         | 
| 120 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 121 | 
            +
              none: false
         | 
| 122 | 
            +
              requirements:
         | 
| 123 | 
            +
              - - ! '>='
         | 
| 124 | 
            +
                - !ruby/object:Gem::Version
         | 
| 125 | 
            +
                  version: '0'
         | 
| 126 | 
            +
                  segments:
         | 
| 127 | 
            +
                  - 0
         | 
| 128 | 
            +
                  hash: 932236595
         | 
| 129 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 130 | 
            +
              none: false
         | 
| 131 | 
            +
              requirements:
         | 
| 132 | 
            +
              - - ! '>='
         | 
| 133 | 
            +
                - !ruby/object:Gem::Version
         | 
| 134 | 
            +
                  version: '0'
         | 
| 135 | 
            +
            requirements: []
         | 
| 136 | 
            +
            rubyforge_project: !!null 
         | 
| 137 | 
            +
            rubygems_version: 1.5.0
         | 
| 138 | 
            +
            signing_key: !!null 
         | 
| 139 | 
            +
            specification_version: 3
         | 
| 140 | 
            +
            summary: LogicalModel allows to use a resource as a model.
         | 
| 141 | 
            +
            test_files: []
         |