Class: Patchboard::Resource
- Inherits:
-
Object
- Object
- Patchboard::Resource
- Defined in:
- lib/patchboard/resource.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#response ⇒ Object
Returns the value of attribute response.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
- .assemble(patchboard, definition, schema, mapping) ⇒ Object
- .decorate(instance, attributes) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #curl ⇒ Object
-
#initialize(context, attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #to_hash ⇒ Object
- #to_json(*a) ⇒ Object
- #values_at(*keys) ⇒ Object
Constructor Details
#initialize(context, attributes = {}) ⇒ Resource
Returns a new instance of Resource.
93 94 95 96 97 |
# File 'lib/patchboard/resource.rb', line 93 def initialize(context, attributes={}) @context = context @attributes = self.class.decorate self, Hashie::Mash.new(attributes) @url = @attributes[:url] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
91 92 93 |
# File 'lib/patchboard/resource.rb', line 91 def attributes @attributes end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
91 92 93 |
# File 'lib/patchboard/resource.rb', line 91 def context @context end |
#response ⇒ Object
Returns the value of attribute response.
90 91 92 |
# File 'lib/patchboard/resource.rb', line 90 def response @response end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
91 92 93 |
# File 'lib/patchboard/resource.rb', line 91 def url @url end |
Class Method Details
.assemble(patchboard, definition, schema, mapping) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/patchboard/resource.rb', line 6 def self.assemble(patchboard, definition, schema, mapping) define_singleton_method(:api) do patchboard.api end define_singleton_method(:mapping) do mapping end define_singleton_method(:schema) do schema end # FIXME: break this out into multiple methods. if schema && schema[:properties] schema[:properties].each do |name, definition| if property_mapping = self.api.find_mapping(definition) if property_mapping.query define_method name do |params={}| params[:url] = @attributes[name][:url] url = property_mapping.generate_url(params) property_mapping.klass.new self.context, :url => url end else define_method name do property_mapping.klass.new self.context, @attributes[name] end end else define_method name do @attributes[name] end end end end # When an additionalProperties schema is defined, the resource can # contain top-level attributes that should obey that schema. if schema && schema[:additionalProperties] != false if (add_mapping = self.api.find_mapping(schema)) && (add_mapping.query) define_method :method_missing do |name, params| params[:url] = @attributes[name][:url] url = add_mapping.generate_url(params) add_mapping.klass.new self.context, :url => url end else define_method :method_missing do |name| @attributes[name.to_sym] end end end define_singleton_method :generate_url do |params| mapping.generate_url(params) end definition[:actions].each do |name, action| action = Action.new(patchboard, name, action) define_method name do |*args| action.request self, @url, *args end end end |
.decorate(instance, attributes) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/patchboard/resource.rb', line 76 def self.decorate(instance, attributes) # TODO: non destructive decoration # TODO: add some sort of validation for the input attributes. # Hey, we have a JSON Schema, why not use it? if self.schema && (properties = self.schema[:properties]) properties.each do |key, sub_schema| if (value = attributes[key]) && !self.api.find_mapping(sub_schema) attributes[key] = self.api.decorate(instance.context, sub_schema, value) end end end attributes end |
Instance Method Details
#[](key) ⇒ Object
107 108 109 |
# File 'lib/patchboard/resource.rb', line 107 def [](key) @attributes[key] end |
#[]=(key, value) ⇒ Object
111 112 113 |
# File 'lib/patchboard/resource.rb', line 111 def []=(key, value) @attributes[key] = value end |
#curl ⇒ Object
116 117 118 |
# File 'lib/patchboard/resource.rb', line 116 def curl raise "Unimplemented" end |
#inspect ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/patchboard/resource.rb', line 99 def inspect id = "%x" % (self.object_id << 1) %Q{ #<#{self.class}:0x#{id} @url="#{@url}"> }.strip end |
#to_hash ⇒ Object
124 125 126 |
# File 'lib/patchboard/resource.rb', line 124 def to_hash self.attributes end |
#to_json(*a) ⇒ Object
128 129 130 |
# File 'lib/patchboard/resource.rb', line 128 def to_json(*a) self.to_hash.to_json(*a) end |
#values_at(*keys) ⇒ Object
120 121 122 |
# File 'lib/patchboard/resource.rb', line 120 def values_at(*keys) self.attributes.values_at(*keys) end |