Skip to content

ktlacaelel/array_attribute

Repository files navigation

array_attribute

Array atrribute accessor helper.

This helper creates two methods to classes.

array_attribute and carray_attribute.

With these you can create methods that encapsulate an array property if you have never used or accessed the property it will have an empty array as a default value.

example.rb

require 'rubygems'
require 'array_attribute'

class Developer
  carray_attribute :fingers
  array_attribute :tickets, :hot_chicks
end

developer = Developer.new

puts developer.tickets.inspect

developer.tickets << 1
developer.tickets << 2
developer.tickets << 3
puts developer.tickets.inspect

developer.tickets.pop
developer.tickets.pop
puts developer.tickets.inspect

developer.tickets.pop
puts developer.tickets.inspect

puts developer.hot_chicks.inspect

developer.hot_chicks << 1
developer.hot_chicks << 2
developer.hot_chicks << 3
puts developer.hot_chicks.inspect

puts Developer.fingers.inspect 

(1..10).to_a.each do |finger|
  Developer.fingers << finger
end

puts Developer.fingers.inspect

output:

[]
[1, 2, 3]
[1]
[]
[]
[1, 2, 3]
[]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Contributing to array_attribute

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2014 kazuyoshi tlacaelel. See LICENSE.txt for further details.

About

Array Attribute - Array atrribute accessor helper.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages