Skip to content

Commit

Permalink
Closes #2 - Adds monkey patching for string
Browse files Browse the repository at this point in the history
  • Loading branch information
armahillo committed Oct 6, 2020
1 parent 94156e2 commit 7e6434c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

N.B. until it hits 1.0 do not make any expectations about API consistency! (SemVer says it's OK so nyah!)

## [0.3.0] - 2020-10-05

- Adds monkey-patch to allow String to call `:emoji_sub` directly

## [0.2.2] - 2020-08-22

- Some reorganization of files, and cleanup
Expand Down Expand Up @@ -42,6 +46,8 @@ emoji HTML entities (hex-unicode) for slack shortcut format (:this_kind:)
- data/emoji.yml file,
- Specs covering basic funcitonality

[0.3.0]: https://github.com/armahillo/emoji_sub/compare/v0.2.2...v0.3.0
[0.2.2]: https://github.com/armahillo/emoji_sub/compare/v0.2.1...v0.2.2
[0.2.1]: https://github.com/armahillo/emoji_sub/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/armahillo/emoji_sub/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/armahillo/emoji_sub/releases/tag/v0.1.0
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ text = "I :heart: New York :statue_of_liberty: :pizza: :pigeon:"
better_text = EmojiSub.emoji_sub(text, additional_emoji)
```

You can also include the monkey-patch for it so that you can call `.emoji_sub` directly on a string. To enable this behavior, add this somewhere in your scripts bootstrap (it just needs to be run before other it's used)

```ruby
String.prepend CoreExtensions::EmojiSub::String
```

### Using Custom Mappings / Overrides

You can even, even override the mappings I did with your own! If your expert-moji opinion says that `:smiley_cat:` should be 😸 and `:smile_cat:` should be 😺 who am I to disagree? They're very different, clearly, and obviously the "-y" on the end of "smile-y" means that the cat should have eyes that are more smiley. Very reasonable and I completely understand! YES I'M FINE WHY DO YOU ASK?

In all seriousness, one big reason I could see wanting to do overrides is because emoji shortcodes in Slack are often quite gendered, such as :man_bouncing_ball: => ⛹ and you'd like to use :woman_bouncing_ball: (⛹), which doesn't exist in the current YML mapping because GOOD GOB THERE ARE SO MANY AND I AM BUT ONE PERSON.

Currently, the emoji are also a single skin-tone. I'm still learning how to combine emoji to do skin-tone modifiers, but when I do, you would also use the overrides thing to do that, as well! If you're an e-moji e-xpert and can help out with this, please do!

Other reasons to override might be because you prefer a specific version of `:airplane:` to be mapped to `:airplane:` instead of ✈, and at the end of the day, this is about making it more convenient for you.


### Skin-tone variants

Currently, the emoji are also a single skin-tone. I'm still learning how to combine emoji to do skin-tone modifiers, but when I do, you would also use the overrides thing to do that, as well! If you're an e-moji e-xpert and can help out with this, please do!

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
19 changes: 19 additions & 0 deletions lib/core_extensions/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module CoreExtensions
module EmojiSub
##
# This monkey patches String so that it can receive emoji_sub directives
# cf. https://github.com/armahillo/emoji_sub/issues/2
# If you want to monkey patch your use of String, you should be able to include
# this module and that should do it.
##
module String
def emoji_sub(options = {})
::EmojiSub.emoji_sub(self, options)
end

def emoji_sub!(options = {})
replace emoji_sub(options)
end
end
end
end
1 change: 0 additions & 1 deletion lib/emoji_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ def emoji_definitions
end

module_function :emoji_sub, :emoji_definitions

end
2 changes: 1 addition & 1 deletion lib/emoji_sub/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EmojiSub
VERSION = "0.2.2"
VERSION = "0.3.0"
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "bundler/setup"
require "emoji_sub"
require 'pry'
require 'binding_of_caller'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
35 changes: 35 additions & 0 deletions spec/string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'core_extensions/string'

RSpec.describe String do
before(:each) do
String.prepend CoreExtensions::EmojiSub::String
end

subject { "I :heartbeat: NY" }

describe "emoji_sub" do
it { is_expected.to be_respond_to(:emoji_sub) }

it "replaces the shortcodes with their corresponding unicode" do
expect(subject.emoji_sub).to eq('I &#x1F493 NY')
end

it "allows an override to be provided as an argument" do
expect(subject.emoji_sub({ heartbeat: "1111" })).to eq('I &#x1111 NY')
end
end

describe "emoji_sub!" do
it { is_expected.to be_respond_to(:emoji_sub!) }

it "replaces in-place with the corresponding unicode" do
subject.emoji_sub!
expect(subject).to eq('I &#x1F493 NY')
end

it "allows an override to be provided as an argument" do
subject.emoji_sub!({ heartbeat: "1111" })
expect(subject).to eq('I &#x1111 NY')
end
end
end

0 comments on commit 7e6434c

Please sign in to comment.