teleren/devto huggingface dataset:
Use Enums for Tags
Tags: swiftui, swift
Published: 2020-05-14 20:23:18

---
title: Use Enums for Tags
description: "This post is going to describe why Enums are perfect for tags in iOS and macOS apps written with SwiftUI."
tags: [SwiftUI, Swift]
published: true
# canonical_url: https://appsbymw.com/blog/2020/05/11/Use-Enums-for-Tags
---

This article assumes you know what an Enum or *Enumeration* in Swift is, and will explain why they are a perfect use case for tags in iOS and macOS development. If you want to read more about Enums, the [Swift website](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html) is a great resource!

# What are tags?

A tag is an `Int` that is assigned in a view. If you create a new iOS app in Xcode that is a Tabbed app, you'll notice that the 2 tab views have a `.tag()` associated with each view. In the gist below, you can see that on lines 16 and 25.

{% gist https://gist.github.com/appsbymw/c3ed2f60785ef20fe0fc547d3c87f0d7 %}

If you have a lot of tabs in this view, then you might get confused about which tag number is associate with which tab. Enums can help make these tags more descriptive and more readable.

# How do Enums help?

Enums have cases that are labeled and can conform to the `Hashable` protocol, which is needed for tags in SwiftUI.

An example of an Enum for views in a todo app can look like this

{% gist https://gist.github.com/appsbymw/657497334a588442929b3e60a89c992e %}

When declaring the enum, we need to make sure that it conforms to the [`Hashable` protocol](https://developer.apple.com/documentation/swift/hashable). We don't need to do anything else besides adopt it since there are no associated values with the cases. If there were associated values, then we would need to make sure those values are conforming to the protocol.

Those enum cases in the gist above are a lot more readable than the numbers. We know from a glance what view will be taken by pressing that tab.

# How to implement the Enum in the app

Now, I am going to implement `Tabs` into the SwiftUI app created earlier. After declaring the enum in `ContentView.swift`, I need to change the `State` to be the inbox view, so the value of state needs to be `Tabs.inbox`. Next, I need to change the tags values to `Tabs.inbox`, `Tabs.projects`, and `Tabs.settings`. The gist below shows the final code of the project.

{% gist https://gist.github.com/appsbymw/d8f7eed6d5adb58ea0ce1d8e4ab0828d %}

Line 4 shows the new `State` assignment and lines 22, 28, and 40 show the new tag values.

Now, it's a bit easier to read which tab is assigned to each view.

The completed project can be cloned from [GitLab](https://gitlab.com/appsbymw/tags-as-enums).

---

If you enjoy my posts, streams, and apps, consider encouraging my efforts.
- <a href="https://www.patreon.com/appsbymw" target="_blank"><button type="button" class="crayons-btn">Become a Patron</button></a>
- <a href="https://www.buymeacoffee.com/appsbymw" target="_blank"><button type="button" class="crayons-btn">Buy Me a Coffee</button></a>
- <a href="https://buttondown.email/appsByMW"><button type="button" class="crayons-btn">Subscribe to my Newsletter!</button></a>
JS visual notes: Data types
Tags: javascript, webdev, beginners
Published: 2020-05-14 20:14:06

Hi, today I want to share with you next 2 notes about data types in JS

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9lkkkmvb9b2gb46bsn6d.jpeg)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/34cjoe9nwbibwxkcyxay.jpeg)
Fake Laravel Eloquent relations on the fly
Tags: laravel, faker
Published: 2020-05-14 20:05:23

Sometimes you just want to quickly demo something, you are not bothered by constantly setting up your data and relationships.

Its actually super easy to just fake your relationships on the Fly:

```php
trait FakeEloquentRelationships {
    public bool $fakingRelations = false;

    protected function getRelationshipFromMethod($method)
    {
        if ($this->fakingRelations) {
            $relationShip = $this->$method();
            if ($relationShip instanceof Relation) {
                $model = $relationShip->getModel();
                $class = get_class($model);
                $factory = app(Factory::class);
                
                $item = $factory->of($class)
                   ->state('full') // i use state 'full' to make sure all properties are set but you can ignore this.
                   ->make();
                $expectSingle = $relationShip instanceof BelongsTo || $relationShip instanceof  HasOne;

                return $expectSingle ? $item : [$item];
            }
        }
        parent::getRelationshipFromMethod($method);
    }
}
```

Now you can use this trait `FakeEloquentRelationships` on your model set `$model->fakingRelations = true` and you are good to go. For example:

```php
class User {
    use FakeEloquentRelationships;

    public function role()
    {
        return $this->belongsTo(Role::class);
    }
}


$user = app(Factory::class)->of(User::class)->make();
$user->fakingRelations = true;
$user->role; // welcome to your completely faked role :)
```

Hope you liked my post.

PS: we actually use this to generate our Open API (swagger) documentation in combination with league/fractal transformers.
Learning to write code that is readable, maintainable, testable, and future changes.
Tags: codequality
Published: 2020-05-14 20:04:03

---
title: Learning to write code that is readable, maintainable, testable, and future changes.
published: true
---
I wonder from time to time whether I'm doing the right thing with respect to guidelines and principles when using an object-oriented approach to create an application. I started reading the Object Design Style Guide book last year to learn more about objects. And beginning this year, I decided to delve deeper into writing code that is readable, maintainable, and testable as well as how to prepare codebase to embrace future changes. 

Here are the resources used:
> Books

- Object Design Style Guide.
- Principles of Package Design.

> Pluralsight Courses

- Writing Readable and Maintainable Code
- SOLID Software Design Principles
- Abstract Art: Getting Things "Just Right"
- Getting Started with Dependency Injection
- Writing Testable Code
- Refactoring: Best Practices

The notes taking while exploring these concepts can be found on this [gist](https://gist.github.com/OliverMensahDev/2385449dc108826c87f885145ae65d04)
Dark mode using prefers-color-scheme rule and CSS variables
Tags: css, design, ux
Published: 2020-05-14 19:55:12

In this article I'll show you how to implement dark mode in your blog or site using [CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) and how the toggle it automatically based on the users [preferred color scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme).

CSS variables are a new way to make CSS more maintainable by defining reusable variables which are defined in one place and can be referenced in multiple other places.

For our dark mode example we'll be changing the sites background and text colors when the user has set dark mode as preference at [system level](https://support.apple.com/en-us/HT208976).

So let's define our variables.

```css
:root {
  --background-color: #1b1b1b;
  --text-color: #d0d0d0;
}
```

Here we've defined two variables `--background-color`and `--text-color` which we'll be using in our CSS definitions. They are added to the `:root` score so they are available globally.

But first we have to add a `@media`queries with the `prefers-color-scheme`rule to enable automatic color scheme selection based on the users settings.

```css
@media (prefers-color-scheme: light) {
  :root {
    --background-color: #ffffff;
    --text-color: #000000;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --background-color: #1b1b1b;
    --text-color: #d0d0d0;
  }
}
```

Now we're ready to use our CSS variables in our definitions.

```css
body {
  background-color: var(--background-color);
  color: var(--text-color);
}
```

Now the user should get the color scheme they prefer based on their system settings.

You can see this code in action on my personal website at [juhanajauhiainen.com](http://juhanajauhiainen.com)

If you liked this article and want to hear more from me, you can follow me on [Twitter](https://twitter.com/juhanakristian)
How is your experience with Youtube Live? Have you been banned during the pandemic?
Tags: discuss, poll, ban, livestream
Published: 2020-05-14 19:43:56

In times of ~~desperation~~ Covid-19, most of us turned to Youtube as the main live stream channel for everything. Recently, I heard about some unusual bans happening around the platform (Hi there,  Remote Python Pizza!). Until last night, when I got banned for no apparent reason since all was marked as "not for kids".
# What brings me to wonder: has this ever happened to you?
Share your stories and let's see if we find out why their AI model is broken.

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Maybe I just want to know why their <a href="https://twitter.com/hashtag/AI?src=hash&amp;ref_src=twsrc%5Etfw">#AI</a> failed 🙊</p>&mdash; Cheuk Ting Ho #StaytheFHome (@cheukting_ho) <a href="https://twitter.com/cheukting_ho/status/1260928822435692545?ref_src=twsrc%5Etfw">May 14, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Piecing Together Bootcamp Knowledge
Tags: bootcamp, learning, mysql, react
Published: 2020-05-14 19:12:24

Perhaps this isn't a unique experience, and everyone faces the same conundrum during their coding careers, but I found the process of graduating a coding bootcamp *very* jarring. You're thrown into the world having learned, at some foundational level, several new technologies. It's up to you to figure out how to leverage those technologies to create applications that you can grow your portfolio with. How are you supposed to take that newfound knowledge and piece it all together?

![Schitt's Creek Overwhelmed](https://media.giphy.com/media/mNdOc0Aziv88E/giphy.gif)

Thinking about this would often times make my head spin. I had several ideas for projects, but wasn't sure how feasible they were with my skill-set. After many failed projects, I realized I had to start small and simply re-learn -- this time in greater detail -- the same concepts we learned in bootcamp. In this series of posts, I document that process, and some things I've discovered along the way.

![Timmy Tommy R Cute](https://media.giphy.com/media/PkKjXRfMCxnRMHQWqE/giphy.gif)

To keep myself motivated, I have chosen a themed app to match my current obsession: **Animal Crossing**. The goal is simple, create a 'dashboard' that will allow me to view my friend's turnip prices and item wish-lists. The application should use MySQL, Node/Express, and React. If you're interested, follow along! In the next post I'll go over creating a basic boilerplate for the application. 

How has your experience been graduating bootcamp?
AMA: Where can we learn Threat Modelling?
Tags: threat, application, security, appsec
Published: 2020-05-14 19:05:40

---
title: AMA: Where can we learn Threat Modelling?
published: true
description: In a recent ‘Ask Me Anything’ Tanya covers ‘Where can we learn Threat Modelling?’.
tags: Threat, Application, Security, AppSec
---

In a recent ‘Ask Me Anything’ Tanya covers [‘Where can we learn Threat Modelling?’](https://community.wehackpurple.com). The linked video is approximately 2 minutes.

 {% youtube UNqokgCI-KI %} 

Where can we learn Threat Modelling?
* Threat modelling, for those who are unaware, is a sort of ‘evil brainstorming’.
* The question included “How can we learn by doing, not just reading?”
Play the game “[Escalation of Privilege](https://github.com/adamshostack/eop)”, create by [Adam Shostack](https://adam.shostack.org)
* You can actually play online, for free! It just came online last week. [Play online here](http://eopgame.herokuapp.com/?s=kG5aZwUukMYvfdTa&c=1&m=66f036b872d48fc2f7a85d3539829059234aee64).
* She also mentions that you should play [Backdoors and Breaches](https://www.blackhillsinfosec.com/projects/backdoorsandbreaches/), however, that is an incident response card game. You should still play it, but it won’t teach you threat modelling. :-D
* Every time there is a new project at work, meet with them for one hour and just *try* to threat model. It’s okay if it’s not perfect, if you identify just one risk you had not thought of, your sessions was productive.
* Every time someone else at work is doing a threat model, sit in and “job shadow” them. Learning by watching and participating is a fantastic way to get in the middle of things.
* Non-hands-on activities: 1) watch the many videos on this topic by several experts in the area, [Adam Shostack](https://adam.shostack.org), [Avi Douglen](https://www.youtube.com/watch?v=3Fl_7FrM_gI&feature=youtu.be), [Tony UcedaVelez](https://owasp.org/www-pdf-archive/AppSecEU2012_PASTA.pdf), [Caroline Moeckel](https://twitter.com/caromoeckel), [Tash Norris](https://twitter.com/TashJNorris), the list goes on and on.
* Whiteboard designs with people and then ‘put on your black hat’ and take a look.
* Ask the tech team (developers, architects, ops peeps), ‘If you were going to hack your app, how would you do it?” The answers may terrify you, but you’ll be happy you asked.
* Read [Tanya Janca’s](https://twitter.com/shehackspurple) numerous articles on the topic: [Hacking Robots and Eating Sushi](https://medium.com/bugbountywriteup/hacking-robots-and-eating-sushi-6a56b6f6a6c9), [Threat Modelling Serverless](https://dev.to/azure/threat-modelling-serverless-500k), and [Threat Modelling](https://dev.to/azure/pushing-left-like-a-boss-part-6-threat-modelling-1j5g).
* Then we get a bit off topic and start talking about Azure DevOps and GitHub Actions…

**For this and more, check out my book, [Alice and Bob Learn Application Security](https://aliceandboblearn.com/) and my online training academy, [We Hack Purple](https://academy.wehackpurple.com)!**


PS The Video Quality is low in this video and has been improved in future recordings.
About Ruby Hash#flatten
Tags: ruby, hash, documentation
Published: 2020-05-14 19:05:38

---
title: About Ruby Hash#flatten
published: true
description: 
tags: ruby, hash, documentation
---

Get more information and more examples at my AboutRuby project:
* [AboutRuby project](https://github.com/BurdetteLamar/AboutRuby/blob/master/core/Hash/api/markdown.md#flatten)

Here's the write-up over at the Ruby project:
* [Ruby project](https://ruby-doc.org/core-2.7.0/Hash.html#method-i-flatten)
Mapbox's Sheet Mapper with D3
Published: 2020-05-13 15:58:32

As [Killed By Google](https://killedbygoogle.com/) dramatically illustrates, Google frequently creates and then shuts down products and APIs. This culture of rapid evolution can leave folks behind if they are not paying attention. Today, I'm taking a look at how this could happen for a nice tool from Mapbox and how to quickly avoid it.

In the spring of 2020, Mapbox introduced [Sheet Mapper](https://www.mapbox.com/impact-tools/sheet-mapper), a tool that displays points of interest (POI) on a map. That covers the mapper part; for the sheet part, the tool uses [Tabletop.js](https://github.com/jsoma/tabletop) to read POI data from a Google Sheet. All told, a quick and dirty visualization tool that requires little programming background to get started.

Subsequently, Mapbox revised Sheet Mapper with [Sheet Mapper Advanced](https://www.mapbox.com/impact-tools/sheet-mapper-advanced-caching), adding caching with S3 and Lambda. In Sheet Mapper Advanced, the app reads POIs from CSV files on S3 using the [D3.js](https://github.com/d3/d3) library. A more scalable and robust solution for sure.

However, the original Sheet Mapper is still a fun tool that unfortunately is going to break as of September 2020 when [Google turns off the version of the Sheets API](https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api) that Tabletop.js uses. The creator of Tabletop.js has a short section in the readme with a workaround using the [Papa Parse](https://www.papaparse.com/) library.

I created a [repo in Github](https://github.com/phm200/sheetmapper-d3), replacing Tabletop.js with D3.js to follow Sheet Mapper Advanced. The changes were minimal, copied from Sheet Mapper Advanced, including publishing the Sheet as CSV and using d3.csv() to load the data.

You can see the result online at https://phm200.github.io/sheetmapper-d3/ and check out the repo for more details.

{% github phm200/sheetmapper-d3 %}
It’s been a while...
Published: 2020-05-14 18:52:38

Well everyone, I’m back! Back in November I was posting about the #100days challenge and I was super excited. Since then I actually received a job offer and began my job as a software developer in December! Starting a new job has obviously been stressful and I feel like I have been doing all my learning while at work and after work I need to recharge so I haven’t been able to work on personal projects and develop other skills outside of work sadly. But starting today I will begin my adventure on continuing to learn and do projects outside of work for fun not just for work! This is super important for me because I feel like I’ve lost a little bit of my drive not being able to experiment and program outside of work. Anyways I hope to continue posting and learning! Thanks for everyone who read my rant I hope you all have a good day and feel free to connect with me!
Tips for testing web applications
Tags: webdev, testing, php, laravel
Published: 2020-05-14 18:51:54

---
title: Tips for testing web applications
published: true
description: Here I share some testing tips that may help you if you're new to TDD
tags: webdev,testing,php,laravel
---

Hey everyone!  
Here are some tips that might help you test your applications. I had it previously posted as a [twitter thread](https://twitter.com/mateusjatenee/status/1260970923999592453). Sadly I could not inline images here so it might be useful to look at the thread.



1. Don't be afraid to hit the database. Today DB calls are very fast, and if you're mocking the database you're usually not testing the feature completely. It makes sense to write "pure" unit tests on lower-level things like libraries, though.
2. Reverse test when testing code that already exists. A easy way to make sure your new tests cover an existing feature is to comment out blocks of code (specially if blocks) and see if the tests break. If they don't, you're missing something. 
3. It's better to write some "unnecessary " tests than not to write them, so If you're wondering wether a test will be useful, just write it. Those few minutes might help you a lot later.
4. Use fakes instead of mocking external services when possible. Laravel ships with many fakes, and when you need a custom one it's easy to swap the implementation for a fake on the container.  That's possible on anything that has a container. The tweet ocntains an example using Laravel's Facades.
5. Tests are important, but don't let them hold you back. If you feel thinking how the API is going to look before writing the code is holding you back, just forget the tests for a moment. You can come back later and write them. Writing them before is not a exactly a rule.
How I Setup A Monitor In My Room And Why It's Awesome
Tags: workstations
Published: 2020-05-14 18:43:00

Ever since schools have been closed, I've had much more time to code and write. 

Online school only takes me 1-2 hours every morning. Before, school in-person before quarantine took at least 11 hours out of my day, leaving me with little time to do what I love (coding, writing, building).

Doing school online and at home isn't the main focus of this article. The main focus is that I setup a monitor in my room and it's helped me get a LOT done.

Let's dive right in, shall we?

Here's my setup in my room:

![my monitor setup in my room](https://joshternyak.com/content/images/2020/05/monitor-setup.JPG)

It's a very simple setup, but it works great. 

I have experimented with many setups in many different rooms, but this one seems to work the best. Since the monitor wasn't tall enough to not be blocked by my Mac computer, I placed the monitor on top of a handful of books. 

Speaking of books, I'm working on making my own reading list. I've read many books this year, and I am reviewing my favorite books and how they've changed my life.

Back to the setup. The table I am using is primarily made to be a picnic table. However, it works GREAT for anyone who just needs a simple table to use for work or school. 

My monitor setup is AWESOME, very simple, and helps me stay productive day-in and day-out. 

## Keeping my room simple

My work room is also my bedroom. Despite what many say about having your work office in your bedroom, I like it for several reasons.

![another view of my monitor setup](https://joshternyak.com/content/images/2020/05/laptop-monitor-setup.JPG)

1. My bedroom is the only room that I can keep under control
2. Every other room in my house is slightly noisy and is distracting
3. I use my work table also as a reading table
4. My room is always clean and neat

## The Gist
That's the gist of it. My monitor setup is simple and helps me stay productive. I keep my room clean and do my best to be productive everyday. 

There are days where I watch an episode of Impact Theory. However, I use it as a time to take a step back from doing something mentally tiring. 

Now I'd like to hear from you.

How did you setup your work environment?

Do you have a monitor as well as a laptop?

Or maybe you only have a laptop?

Either way, let me know by leaving a comment, or [tweeting at me](https://twitter.com/josh_ternyak).
wtf is a feature flag?
Tags: ruby, beginners, rails
Published: 2020-05-14 18:31:24

![bulldozer](https://media.giphy.com/media/7ieIikuzaJ67qNlYhz/giphy.gif)

[rollout.io](https://rollout.io/)
[optimizely.com](https://www.optimizely.com/rollouts-signup)
[launchdarkly](https://launchdarkly.com/features/feature-flags)

The above services are bulldozers for many applications. Large companies rely on their added features like shiny dashboards and sugar methods.

#### Andy's dictionary:
> **Feature flag**: allows your system to *conditionally show* a feature to a customer.

Feature flags are an abstraction on top of your basic programming conditionals. They house the logic of your underlying features a customer should or should not be able to access.

Imagine a scenario where you're wrapping up a feature for your SaSS. It's ready to go, however - you'd be more confident if a few customers can test it out.

More or less... beta testing.

The above services can prove to be hefty for smaller projects. You might not have many users yet. Your codebase might not be the size of an enterprise. Did I mention those services are mostly bulldozers? Where the shovel at?

I got your shovel. Integrating feature flags to Ruby projects can be accomplished using [Rollout](https://github.com/fetlife/rollout). It's minimal in it's approach and reads well. It doesn't hurt that the project has few outstanding Github issues in it's queue.

Rollout also scales well. [Kajabi](kajabi.com) has a generous user base. We're releasing features non stop! We've built features on top of Rollout for our needs and it's still in use, today.

Back to our contrived example, let's say you're adding an affiliate program. You have a founders group and want to ensure they get their hands on it, first! 

After installing the gem, define/activate your group and use conditionally write your markup like so:

```ruby
# config/initializers.rb
$rollout.define_group(:founders) do |user|
  user.plan_name == "founder"
end

$rollout.activate_group(:affiliate_program, :founders)
```

```html
<!-- index.html.erb -->
<html>
  <body>
    <% if $rollout.active?(:affiliate_program) %>
      <%= render "affiliate_program" %>
    <% else %>
      <%= render "something_else" %>
    <% end %>
  </body>
</html>
```