lib/resource_kitling.rb in freefeed-client-0.1.0 vs lib/resource_kitling.rb in freefeed-client-1.1.0

- old
+ new

@@ -1,10 +1,10 @@ module ResourceKitling require 'forwardable' class ActionCollection extend Forwardable - def_delegators :@collection, :find, :<<, :each, :include? + def_delegators :@collection, :find, :<<, :each, :include?, :map def initialize @collection = [] end def action(name, &block) @@ -44,10 +44,14 @@ def generated_path(values = {}) values.inject("/#{version}/#{path}") do |np, (key, value)| np.gsub(":#{key}", value.to_s) end end + + def to_s + name + end end class Resource class << self attr_accessor :_actions attr_reader :subclasses @@ -60,27 +64,27 @@ def self.actions(&block) self._actions ||= ActionCollection.new if block_given? self._actions.instance_eval(&block) - # MethodFactory.construct(self, self._actions) self._actions.each do |action| - if method_defined?(action.name) + if respond_to?(action.name.to_sym) raise( ArgumentError, "Action '#{action.name}' is already defined on `#{self}`" ) end method_block = method_for_action(action) - send(:define_method, action.name, &method_block) + send(:define_singleton_method, action.name, &method_block) end end self._actions end def self.method_for_action(action) Proc.new do |*args| + client = args.shift pathopts = action.path.include?(':') ? args.shift : {} payload = args.shift payload = payload.to_h if payload.respond_to?(:to_h) client.call!( action.verb, action.generated_path(pathopts), payload, *args @@ -90,12 +94,9 @@ def actions self.class.actions end - attr_reader :client - - def initialize(client: nil) - @client = client + def initialize end end end