Skip to content

frontfoot/random_attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RandomAttributes

Sometimes people give you rubbish data, when they do rather than wiring everything up with hundreds of methods you could use RandomAttributes.

If mangling crappy data is not your issue you are probably better served by something like virtus.

Installation

Add this line to your application's Gemfile:

gem 'random_attributes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install random_attributes

Usage

Vanilla:

class Foo
  include RandomAttributes
  attribute "fooBar"
end

foo = Foo.parse { "fooBar" => "hello" }
foo.foo_bar #=> "hello"

Alias:

class Foo
  include RandomAttributes
  attribute "fooBar", alias: :moo_bar
end

foo = Foo.parse { "fooBar" => "hello" }
foo.moo_bar #=> "hello"

Type casting:

class Foo
  include RandomAttributes
  attribute "fooBar", type: String
end

foo = Foo.parse { "fooBar" => 34 }
foo.foo_bar #=> "34"

Many possible values:

class Foo
  include RandomAttributes
  attribute ["fooBar", "mooBar"], alias: :foo_bar
end

foo = Foo.parse { "fooBar" => "hello" }
foo.foo_bar #=> "hello"

foo = Foo.parse { "mooBar" => "hello" }
foo.foo_bar #=> "hello"

Trying another node:

class Foo
  include RandomAttributes
  attribute "parentNode"
  attribute "fooBar", try: :parent_node
end

foo = Foo.parse { "fooBar" => "hello" }
foo.foo_bar #=> "hello"

foo = Foo.parse { "parentNode" => { "fooBar" => "hello" } }
foo.foo_bar #=> "hello"

Nested within a node:

class Foo
  include RandomAttributes
  attribute "parentNode"
  attribute "fooBar", within: :parent_node
end

foo = Foo.parse { "parentNode" => { "fooBar" => "hello" } }
foo.foo_bar #=> "hello"

Models:

class Bar
  attr_reader :message
  def initialize message
    @message = message
  end
end

class Foo
  include RandomAttributes
  attribute "fooBar", model: Bar
end

foo = Foo.parse { "fooBar" => "hello" }
foo.foo_bar.message #=> "hello"

Collections:

class Bar
  attr_reader :message
  def initialize message
    @message = message
  end
end

class Foo
  include RandomAttributes
  attribute "fooBar", collection: Bar
end

foo = Foo.parse { "fooBar" => ["hello", "goodbye"] }
foo.foo_bar.first.message #=> "hello"
foo.foo_bar.last.message #=> "goodbye"

Parse data with proc:

class Foo
  include RandomAttributes
  attribute "fooBar", parse: ->(value) { "#{value} from a proc!" }
end

foo = Foo.parse { "fooBar" => "hello" }
foo.foo_bar #=> "hello from a proc!"

Callbacks:

class Foo
  include RandomAttributes
  attribute "fooBar"
  attribute "multipleFooBar"

  after_parse :multiply_foo_bar

  def multiply_foo_bar
    set_attribute :multiple_foo_bar, foo_bar * 2
  end
end

foo = Foo.parse { "fooBar" => 2 }
foo.multiple_foo_bar #=> 4

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages