Class: MtgApi::Config
- Inherits:
-
Object
- Object
- MtgApi::Config
- Defined in:
- lib/mtg_api/config.rb
Overview
the configured properties of an entity
Instance Attribute Summary collapse
-
#attributes ⇒ Object
the stored properties of this config.
-
#name ⇒ Object
the stored properties of this config.
-
#properties ⇒ Object
the stored properties of this config.
-
#setters ⇒ Object
the stored properties of this config.
Instance Method Summary collapse
-
#accessors ⇒ Object
the list of attributes to build attr_accessors for.
-
#attribute(*attributes) ⇒ Object
add to the attributes list (queryable).
-
#endpoint ⇒ Object
the endpoint to send to the api for the entity.
-
#full_config ⇒ Object
the full list of attributes set.
-
#initialize(clazz) ⇒ Config
constructor
store the class name and initialize empty lists.
-
#property(*properties) ⇒ Object
add to the properties list (unqueryable).
-
#response_key ⇒ Object
the key in the response for the api.
-
#setter(attribute, clazz = nil, &block) ⇒ Object
build a setter that can map the return value from the api.
Constructor Details
#initialize(clazz) ⇒ Config
store the class name and initialize empty lists
10 11 12 13 14 15 |
# File 'lib/mtg_api/config.rb', line 10 def initialize(clazz) self.name = clazz.name self.attributes = [] self.properties = [] self.setters = {} end |
Instance Attribute Details
#attributes ⇒ Object
the stored properties of this config
7 8 9 |
# File 'lib/mtg_api/config.rb', line 7 def attributes @attributes end |
#name ⇒ Object
the stored properties of this config
7 8 9 |
# File 'lib/mtg_api/config.rb', line 7 def name @name end |
#properties ⇒ Object
the stored properties of this config
7 8 9 |
# File 'lib/mtg_api/config.rb', line 7 def properties @properties end |
#setters ⇒ Object
the stored properties of this config
7 8 9 |
# File 'lib/mtg_api/config.rb', line 7 def setters @setters end |
Instance Method Details
#accessors ⇒ Object
the list of attributes to build attr_accessors for
18 19 20 |
# File 'lib/mtg_api/config.rb', line 18 def accessors attributes + properties - setters.keys end |
#attribute(*attributes) ⇒ Object
add to the attributes list (queryable)
23 24 25 |
# File 'lib/mtg_api/config.rb', line 23 def attribute(*attributes) self.attributes += attributes end |
#endpoint ⇒ Object
the endpoint to send to the api for the entity
28 29 30 |
# File 'lib/mtg_api/config.rb', line 28 def endpoint @endpoint ||= '/' + response_key end |
#full_config ⇒ Object
the full list of attributes set
33 34 35 |
# File 'lib/mtg_api/config.rb', line 33 def full_config (attributes + properties + setters.keys).uniq end |
#property(*properties) ⇒ Object
add to the properties list (unqueryable)
38 39 40 |
# File 'lib/mtg_api/config.rb', line 38 def property(*properties) self.properties += properties end |
#response_key ⇒ Object
the key in the response for the api
43 44 45 |
# File 'lib/mtg_api/config.rb', line 43 def response_key @response_key ||= name.downcase.split('::').last + 's' end |
#setter(attribute, clazz = nil, &block) ⇒ Object
build a setter that can map the return value from the api
48 49 50 51 52 53 |
# File 'lib/mtg_api/config.rb', line 48 def setter(attribute, clazz = nil, &block) setters[attribute] = proc do |value| value = clazz.nil? ? block.call(value) : clazz.new(value) instance_variable_set(:"@#{attribute}", value) end end |