Just did this in my constance gem, and totally love it as a way to add
configuration or options configurability to your gem:
http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.html
like:
module MyModule
class << self
attr_accessor :debug, :proc, :some_mapping
def configure(&block); yield; end
end
end
I''ve seen people that use OpenStruct, Struct, and I had my own way to
do it
before this, but this seems a lot better.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/ojeZ8-j7EEYJ.
For more options, visit https://groups.google.com/groups/opt_out.
Ilia Bylich
2012-Sep-23 12:11 UTC
Re: A super-easy and great way to implement Gem configuration
I am not sure that this way is simple. I usually use for gem configuration
this code
require ''active_support/configurable''
module GemModule
include ActiveSupport::Configurable
class << self
def setup
yield config
end
end
end
And then you can write
GemModule.setup do |config|
config.option1 = "value1"
config.option2 = 2
end
And
GemModule.config.option1
# => "value1"
Moreover, you can define method_missing in you module and delegate unknown
methods to self.config. Then you will be able to write
GemModule.option1
# => "value1"
http://api.rubyonrails.org/classes/ActiveSupport/Configurable.html
суббота, 22 сентября 2012 г., 18:38:22 UTC+3 пользователь gsw
написал:>
> Just did this in my constance gem, and totally love it as a way to add
> configuration or options configurability to your gem:
>
>
http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.html
>
> like:
>
> module MyModule
> class << self
> attr_accessor :debug, :proc, :some_mapping
> def configure(&block); yield; end
> end
> end
>
>
> I''ve seen people that use OpenStruct, Struct, and I had my own way
to do
> it before this, but this seems a lot better.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/AE0JhCfmW6oJ.
For more options, visit https://groups.google.com/groups/opt_out.
On Sunday, September 23, 2012 8:11:43 AM UTC-4, Ilia Bylich wrote:> > I am not sure that this way is simple. I usually use for gem configuration > this code > > require ''active_support/configurable'' > module GemModule > include ActiveSupport::Configurable > > class << self > def setup > yield config > end > end > end > > And then you can write > > GemModule.setup do |config| > config.option1 = "value1" > config.option2 = 2 > end > > And > > GemModule.config.option1 > # => "value1" > > Moreover, you can define method_missing in you module and delegate unknown > methods to self.config. Then you will be able to write > GemModule.option1 > # => "value1" > > http://api.rubyonrails.org/classes/ActiveSupport/Configurable.html > >That works, but it has the following disadvantages over the method I described: 1. If you use ActiveSupport::Configurable you are tied to using activesupport/Rails. Many times you want to write the Gem so that it isn''t Rails-specific and want to limit dependencies. 2. I know Rails uses the "pass the config into the block thing", but it isn''t as DRY as not typing "config." before each parameter. 3. By defining the attr_accessors, you limit the chance that a user will mistype/misspell a variable. 4. The way you described allows the user to define config parameters with procs and lambdas, but the way I describe allows the user to define "config parameters" as procs, lambdas, and methods, i.e. the user can actually just define a variable as: def some_variable_name # some really involved implementation here, even refactored into various methods, etc. in the block. end See more at: http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.html Hope that helps. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zcMdqXgj6gYJ. For more options, visit https://groups.google.com/groups/opt_out.
Just as an update, I had bugs in the previous code provided and fixed with
help from Jesús Gabriel y Galán and Calvin B. on the list in this post:
http://www.ruby-forum.com/topic/4406183
module MyModule
class << self
attr_accessor :debug, :proc_or_lambda, :some_array, :some_hash, :time
def configure(&blk); class_eval(&blk); end
end
end
and can configure like:
MyModule.debug = true
MyModule.proc_or_lambda = lambda{|a,b| puts "a=#{a} b=#{b}"}
# ...
or
MyModule.configure do
@debug = true
@proc_or_lambda = lambda{|a,b| puts "a=#{a} b=#{b}"}
@some_array = [:foo,:bar]
@some_mapping = {:key => ''value''}
def self.time; Time.now; end
end
or
MyModule.configure do
self.debug = true
self.proc_or_lambda = lambda{|a,b| puts "a=#{a} b=#{b}"}
self.some_array = [:foo,:bar]
self.some_mapping = {:key => ''value''}
def self.time; Time.now; end
end
Posted here with more examples:
http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.html
On Sunday, September 23, 2012 11:56:49 AM UTC-4, gsw
wrote:>
> On Sunday, September 23, 2012 8:11:43 AM UTC-4, Ilia Bylich wrote:
>>
>> I am not sure that this way is simple. I usually use for gem
>> configuration this code
>>
>> require ''active_support/configurable''
>> module GemModule
>> include ActiveSupport::Configurable
>>
>> class << self
>> def setup
>> yield config
>> end
>> end
>> end
>>
>> And then you can write
>>
>> GemModule.setup do |config|
>> config.option1 = "value1"
>> config.option2 = 2
>> end
>>
>> And
>>
>> GemModule.config.option1
>> # => "value1"
>>
>> Moreover, you can define method_missing in you module and delegate
>> unknown methods to self.config. Then you will be able to write
>> GemModule.option1
>> # => "value1"
>>
>> http://api.rubyonrails.org/classes/ActiveSupport/Configurable.html
>>
>>
> That works, but it has the following disadvantages over the method I
> described:
>
> 1. If you use ActiveSupport::Configurable you are tied to using
> activesupport/Rails. Many times you want to write the Gem so that it
isn''t
> Rails-specific and want to limit dependencies.
> 2. I know Rails uses the "pass the config into the block thing",
but it
> isn''t as DRY as not typing "config." before each
parameter.
> 3. By defining the attr_accessors, you limit the chance that a user will
> mistype/misspell a variable.
> 4. The way you described allows the user to define config parameters with
> procs and lambdas, but the way I describe allows the user to define
"config
> parameters" as procs, lambdas, and methods, i.e. the user can actually
just
> define a variable as:
>
> def some_variable_name
> # some really involved implementation here, even refactored into various
> methods, etc. in the block.
> end
>
> See more at:
>
http://stufftohelpyouout.blogspot.com/2012/09/forget-struct-and-openstruct-flexible.html
>
> Hope that helps.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/1HjVIJoIanAJ.
For more options, visit https://groups.google.com/groups/opt_out.