Skip to content

Commit 65845bb

Browse files
committed
📝 Document usage
1 parent 99bcd25 commit 65845bb

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ and provide a nice psuedo-object interface.
55

66
It has its roots in the `Rash` (specifically the [`rash_alt`](https://github.com/shishi/rash_alt) flavor), which is a special `Mash`, made popular by the `hashie` gem.
77

8-
`SnakyHash::Snake` does inherit from `Hashie::Mash` and adds some additional behaviors.
8+
Classes that include `SnakyHash::Snake` should inherit from `Hashie::Mash`.
99

1010
## Installation
1111

@@ -19,7 +19,45 @@ If bundler is not being used to manage dependencies, install the gem by executin
1919

2020
## Usage
2121

22-
For now, please see specs
22+
```ruby
23+
class MySnakedHash < Hashie::Mash
24+
include SnakyHash::Snake.new(key_type: :string) # or :symbol
25+
end
26+
27+
snake = MySnakedHash.new(a: 'a', 'b' => 'b', 2 => 2, 'VeryFineHat' => 'Feathers')
28+
snake.a # => 'a'
29+
snake.b # => 'b'
30+
snake[2] # 2
31+
snake.very_fine_hat # => 'Feathers'
32+
snake[:very_fine_hat] # => 'Feathers'
33+
snake['very_fine_hat'] # => 'Feathers'
34+
```
35+
36+
Note above that you can access the values via the string, or symbol.
37+
The `key_type` determines how the key is actually stored, but the hash acts as "indifferent".
38+
Note also that keys that do not respond to `to_sym`, because they don't have a natural conversion to a Symbol,
39+
are left as-is.
40+
41+
### Stranger Things
42+
43+
I don't recommend using these features... but they exist (for now).
44+
You can still access the original un-snaked camel keys.
45+
And through them you can even use un-snaked camel methods.
46+
47+
```ruby
48+
snake.key?('VeryFineHat') # => true
49+
snake['VeryFineHat'] # => 'Feathers'
50+
snake.VeryFineHat # => 'Feathers', PLEASE don't do this!!!
51+
snake['VeryFineHat'] = 'pop' # Please don't do this... you'll get a warning, and it works (for now), but no guarantees.
52+
# WARN -- : You are setting a key that conflicts with a built-in method MySnakedHash#VeryFineHat defined in MySnakedHash. This can cause unexpected behavior when accessing the key as a property. You can still access the key via the #[] method.
53+
# => "pop"
54+
snake.very_fine_hat = 'pop' # => 'pop', do this instead!!!
55+
snake.very_fine_hat # => 'pop'
56+
snake[:very_fine_hat] = 'moose' # => 'moose', or do this instead!!!
57+
snake.very_fine_hat # => 'moose'
58+
snake['very_fine_hat'] = 'cheese' # => 'cheese', or do this instead!!!
59+
snake.very_fine_hat # => 'cheese'
60+
```
2361

2462
## Development
2563

0 commit comments

Comments
 (0)