Module: Balanced::Resource::ClassMethods
- Defined in:
- lib/balanced_ach/resources/resource.rb
Instance Method Summary collapse
- #all(options = {}) ⇒ Object
- #collection_name ⇒ Object
- #collection_path ⇒ Object
- #construct_from_response(payload) ⇒ Object
- #find(*arguments) ⇒ Object
- #member_name ⇒ Object
- #paginate(options = {}) ⇒ Object (also: #scoped, #where)
- #resource_name ⇒ Object
- #uri ⇒ Object
Instance Method Details
#all(options = {}) ⇒ Object
149 150 151 152 |
# File 'lib/balanced_ach/resources/resource.rb', line 149 def all = {} pager = paginate() pager.to_a end |
#collection_name ⇒ Object
74 75 76 |
# File 'lib/balanced_ach/resources/resource.rb', line 74 def collection_name Utils.pluralize Utils.underscore(resource_name) end |
#collection_path ⇒ Object
78 79 80 |
# File 'lib/balanced_ach/resources/resource.rb', line 78 def collection_path ["/#{Balanced.config[:version]}", collection_name].compact.join '/' end |
#construct_from_response(payload) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/balanced_ach/resources/resource.rb', line 90 def construct_from_response payload payload = Balanced::Utils.hash_with_indifferent_read_access payload return payload if payload[:uri].nil? klass = Balanced.from_uri(payload[:uri]) instance = klass.new payload payload.each do |name, value| klass.class_eval { attr_accessor name.to_s } # here is where our interpretations will begin. # if the value is a sub-resource, lets instantiate the class # and set it correctly if value.instance_of? Hash and value.has_key? 'uri' value = construct_from_response value elsif name =~ /_uri$/ modified_name = name.sub(/_uri$/, '') klass.instance_eval { define_method(modified_name) { values_class = Balanced.from_uri(value) # if uri is a collection -> this would definitely be if it ends in a symbol # then we should allow a lazy executor of the query pager if Balanced.is_collection(value) pager = Pager.new value, {} pager.to_a else values_class.find(value) end } } end instance.class.instance_eval { define_method(name) { @attributes[name] } # Get. define_method("#{name}=") { |value| @attributes[name] = value } # Set. define_method("#{name}?") { !!@attributes[name].nil? } # Present. } instance.send("#{name}=".to_s, value) end instance end |
#find(*arguments) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/balanced_ach/resources/resource.rb', line 131 def find *arguments scope = arguments.slice!(0) = arguments.slice!(0) || {} case scope when :all then all() when :first then paginate().first else response = Balanced.get scope, construct_from_response response.body end end |
#member_name ⇒ Object
82 83 84 |
# File 'lib/balanced_ach/resources/resource.rb', line 82 def member_name Utils.underscore resource_name end |
#paginate(options = {}) ⇒ Object Also known as: scoped, where
143 144 145 |
# File 'lib/balanced_ach/resources/resource.rb', line 143 def paginate = {} Pager.new uri, end |
#resource_name ⇒ Object
70 71 72 |
# File 'lib/balanced_ach/resources/resource.rb', line 70 def resource_name Utils.demodulize name end |
#uri ⇒ Object
86 87 88 |
# File 'lib/balanced_ach/resources/resource.rb', line 86 def uri collection_path end |