Last active
August 29, 2015 13:59
-
-
Save nicholaides/10610356 to your computer and use it in GitHub Desktop.
Configuration comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'attributes' | |
module StackerBee | |
class Configuration | |
include Attributes | |
attribute :ssl_verify?, default: proc { true } | |
attribute :url | |
attribute :secret_key | |
attribute :api_key | |
attribute :middlewares, default: proc { proc {} } | |
attribute :faraday_middlewares, default: proc { proc {} } | |
alias_method :to_hash, :attributes | |
def merge(other) | |
self.class.new(to_hash.merge(other.to_hash)) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rubocop:disable EmptyLineBetweenDefs,SingleLineMethods | |
module StackerBee | |
class Configuration | |
def initialize(attrs = nil) | |
@attributes = {} | |
attrs ||= {} | |
attrs.each_pair { |attr, value| send "#{attr}=", value } | |
end | |
def ssl_verify?; read_attribute(:ssl_verify, true) end | |
def ssl_verify=(val) write_attribute :ssl_verify, val end | |
def url; read_attribute(:url) end | |
def url=(val) write_attribute :url, val end | |
def secret_key; read_attribute(:secret_key) end | |
def secret_key=(val) write_attribute :secret_key, val end | |
def api_key; read_attribute(:api_key) end | |
def api_key=(val) write_attribute :api_key, val end | |
def middlewares; read_attribute(:middlewares, proc {}) end | |
def middlewares=(val) write_attribute :middlewares, val end | |
def faraday_middlewares; read_attribute(:faraday_middlewares, proc {}) end | |
def faraday_middlewares=(val) write_attribute :faraday_middlewares, val end | |
def to_hash | |
@attributes | |
end | |
def merge(other) | |
self.class.new(to_hash.merge(other.to_hash)) | |
end | |
private | |
def read_attribute(key, value = nil) | |
@attributes.fetch(key, value) | |
end | |
def write_attribute(*args) | |
@attributes.store(*args) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment