Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly unwanted use of class instead of Module for namespacing #3

Open
Xasin opened this issue Jul 18, 2018 · 3 comments
Open

Possibly unwanted use of class instead of Module for namespacing #3

Xasin opened this issue Jul 18, 2018 · 3 comments

Comments

@Xasin
Copy link

Xasin commented Jul 18, 2018

In the code above, it looks as if you want to put the SPI code into the "SPI" namespace, however, used a ruby class.
This might work for your code, but I've found it to make it impossible to actually reach SPI::Driver::SPIdev without first calling SPI.new(), since that somehow makes things reachable ...

See code attached below:

irb(main):001:0> require 'spi'
=> true
irb(main):002:0> SPI::Driver.constants
=> [:Base]
irb(main):003:0> SPI.new(device: "Something")
SPI::SPIException: Device /dev/spidev32766.0 not found
irb(main):004:0> SPI::Driver.constants
=> [:Base, :SPIdev]
irb(main):005:0> 

@Xasin
Copy link
Author

Xasin commented Jul 18, 2018

Actually, I'd be willing to take this over and clean it up a bit, if that sounds alright.
It's not a lot of code to maintain, and the bulk of the work is done by the spidev kernel module anyways.

@m1ari
Copy link
Owner

m1ari commented Jul 31, 2018

The design was based on the i2c library https://github.com/cho45/ruby-i2c-devices with the intention that more driver classes could be added (and hopefully a better mechanism to load the most suitable one).

Similarly the intention was that calls would normally be done via the main SPI class rather than directly against a driver - although I realise the README might not be so clear on that.

I'm certainly happy to take some PRs to try and clean things up and improve them.

@Xasin
Copy link
Author

Xasin commented Jul 31, 2018

I've looked through some examples, and usually, stacking modules in classes is O.K. to do if the modules inside the class are some form of MixIn
However, loading and autoloading of classes with those modules in them won't properly work if a class is used, since any class MyClass will cause the class to be defined, even though its contents may not be declared properly.

I've found I2C to work better for my code, and my exams at Uni won't make it easy to get some time to make a PR here
But I do recommend using modules for namespacing, and then just having a:

module SPI
  def self.new
  end
end

Where this function returns an instance of a driver class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants