0

I have an engine whom, in the lib/ directory has a configuration file as such:

# Default configration for groups, roles and permissions
class Xaaron::Configuration

  # What is the memeber group name?
  attr_accessor :member_group

  # What is the memeber role name?
  attr_accessor :member_role

  # What is the admin group name?
  attr_accessor :admin_group

  # What is the admin role name?
  attr_accessor :admin_role

  # What is the can read permission name?
  attr_accessor :can_read

  # What is the can create permission name?
  attr_accessor :can_create

  # What is the can edit permission name?
  attr_accessor :can_edit

  # What is the can delete permission name?
  attr_accessor :can_destroy

  # Should we push this data to black bird? False by default.
  attr_accessor :push_to_black_bird

  # Defaults are set here.
  def initialize
    @member_group = 'Member'
    @admin_group = 'Administrator'
    @member_role = 'Member'
    @admin_role = 'Administrator'

    @can_read = 'can_read'
    @can_create = 'can_create'
    @can_edit = 'can_edit'
    @can_destroy = 'can_destroy'

    @push_to_black_bird = false
  end
end

As you can see I have set the default values, incase you don't decide to change these. Now in this engines seed.rb file I am able to do something like:

admin_group = Xaaron::Group.create(:group_name => Xaaron.config.admin_group, :can_alter => false, :can_delete => false)

And it works as intended, we see that Administrator is set as the group name.

Now I tried to create the following, in the app that this engine is mounted in:

Xaaron.configure do |config|
  config.push_to_black_bird = true
end

But when I booted the app, I got:

`block in <top (required)>': undefined method `push_to_black_bird=' for #<Xaaron::Configuration:0x007fbc6b305988> (NoMethodError)

Do I really have to define set and get methods for each of theses? I have seen other configuration files done in similar fashion.

TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • Could you please share `configure` method code? Would be helpful. – markets Jan 26 '15 at 20:14
  • @market, I am? Its `Xaaron::Configuration` Class. Does it need to be `Xaaron.configuration`? If so Why are my seeds working? Because if I do `puts Xaaron.config.admin_group` I get `"Administrator"` in the engine and in the app the engine is mounted in. – TheWebs Jan 26 '15 at 21:23
  • In Ruby `attr_accessor ` defines getters and setters, so you don't have to define them. The point is to see what this `configure` method does. Example code: https://github.com/markets/invisible_captcha/blob/master/lib/invisible_captcha.rb#L26-L28. Example usage: https://github.com/markets/maily#initialization-and-configuration – markets Jan 26 '15 at 21:35
  • I dont actually have any kind of "`configure`" method set or defined. yet its some how magically working when I do `Xaaron.config.admin_group` I can get the value, I just cannot set the value by doing `Xaaron.configure do |config| ... end` @markets - Should it be `Xaaron.initialize do |config| ... end` – TheWebs Jan 26 '15 at 21:40
  • Definitely it's not easy without see more code... is this Xaaron library open-source? Anyway, you're defining accessors for `Xaaron::Configuration` class, not `Xaaron`, so you problably should do something like: `Xaaron::Configuration.configure do ...`. Or may be `Xaaron.configure do ...` is wrapping this inside, that's why I'm asking for that method. – markets Jan 26 '15 at 22:01
  • all the code I have posted is all I have for setting up initializer. I found doing `Xaaron.configuration` to work. As stated there is no method, as you can see in `Xaaron::Configuration` called `configure`. Also this project is not open source. – TheWebs Jan 26 '15 at 22:03
  • In that case the `configure` method should be into `Xaaron` class (not into `Xaaron::Configuration`). Methods should be defined or injected or meta-programmed somewhere... – markets Jan 26 '15 at 22:17

1 Answers1

-1

Maybe this is the wrong approach but this is how I got it to work:

Xaaron.configure do |config|
  Xaaron.configuration.admin_group = 'Sample' # Prev: Administrator
end

puts Xaaron.config.admin_group #=> Sample, was Administrator.
TheWebs
  • 12,470
  • 30
  • 107
  • 211
  • In this case, you problably don't need to wrap the config into that block, since you're accessing to another scope (you don't use `config` variable inside the block). `Xaaron.configuration.admin_group = 'Sample'` should be enough. – markets Jan 26 '15 at 22:20